开发者

Join table attribute updates in Many-to-Many Relation

I have a many-to-many relation setup for a data model which deals with a problem domain of Members and Addresses. Basically a 'member' can have multiple 'addresses', think of like when you have a work address and a home address.

I want to store additional attributes on the join table and save data, apart from the foreign key attributes. There are many examples on the net about many-to-many relations, but I've not found details/examples on storing the extra data.

Here is the problem that i'm stuck with.

The models are setup as:

Models

class Member < ActiveRecord::Base
   has_many :member_addresses
   has_many :addresses, :through => :member_addresses
end

class MemberAddress < ActiveRecord::Base
   belongs_to :address   # foreign key for address -> address_id
   belongs_to :member    # foreign key for member ->member_id
end

class Address < ActiveRecord::Base
   has_many :member_addresses
   has_many :members, :through =开发者_运维问答> :member_addresses

   accepts_nested_attributes_for :members, :allow_destroy => true
end

The 'accepts_nested_attributes' on the Address class is for the nested-forms.

Database Schema/Migrations The tables (simplified for posting) are defined as

create_table "addresses", :force => true do |t|
    t.string  "firstline"
    t.string  "state"
    ....
end

create_table "member", :force => true do |t|
    t.string "name"
    ....
end

create_table "member_addresses", :force => true do |t|
   t.integer "member_id"
   t.integer "address_id"
   t.string  "address_type"
end

The field 'address_type' on the join table is intended for flagging the type of the address that is associated with the member (aka; "home" or "work")

It is updating this field that is where I'm getting stuck.

Controllers & View

The member_controller setups the model like:

members_controller.rb
def new
   @member = Member.new
   @member.addresses.build    #pre-build the associated address
end

Then the view for the member page new action is

<%= form_for @member do |m| %>

  <%= m.label :first_name, "First Name"%>
  <%= m.text_field :first_name %>
  </p>

  <%= m.fields_for :addresses do |addrform|%>
    <%= addrform.label :firstline %>
    <%= addrform.text_field :firstline %>
  <% end %>

  <br />
  <%= label_tag 'Type of address' %>
  <%= text_field_tag 'addrtype'    %>
  <br />
  <%= m.submit "Add" %><br />

<% end %>

The idea is that with the 'accepts_nested_attributes' on the Member model I can enter the data on the form to add in the name and address attributes. The additional label_tag is for the data to be posted for the address type that I want to store

So when the form is filled in the post data (form a trap) looks like:

{"utf8"=>"✓",
     "authenticity_token"=>"E0R038rcdJmBGjjxQ9nQLHGVbzM4ejA0vsEaIvqkwkE=",
     "member"=>{"first_name"=>"James",
     "last_name"=>"SMith",
     "addresses_attributes"=>{"0"=>{"firstline"=>"this is the first line"}}},
     "addrtype"=>"home",
     "commit"=>"Add"}

All good - i'm getting my Member data and the Address data, as well as the additional field 'addrtype' which is to be used for carrying my additional data

Now how to get the data into the relation or join table ? On the member_controller create action

def create
   # create a member with the params posted 
   @member = Member.new(params[:member])

   # get data for 'relation data'
   @addr_type = params[:addrtype]



  if @member.save!
      #update the relation now created with the address type
      @member.member_addresses.first.address_type = @addr_type

      # save the relationship (is this necessary?)
      @member.save!

      redirect_to members_path
  end

As you can see in the create I'm attempting to fill in the data on the join table and then force a save again so that the relation or association with the additional attribute is saved.

This doesn't work, and/or I'm doing it wrong.

Any suggestions would be most appreciated.


Try changing your models to something like this:

class Member < ActiveRecord::Base
   has_many :member_addresses
   accepts_nested_attributes_for :member_addresses, :allow_destroy => true
end

class MemberAddress < ActiveRecord::Base
   belongs_to :member    
   has_many :addresses
   accepts_nested_attributes_for :addresses, allow_destroy => true
end

class Address < ActiveRecord::Base
   belongs_to :member_addresses
end

And change your member_controller.create method to

@member = Member.new(params[:member])

This should allow you to create MemberAddresses and Addresses via the MemberController.create method.

These 2 RailsCasts might also be worth a look

#196 Nested Model Form Part 1
#197 Nested Model Form Part 2


Not as fluent in Rails as I would like to be, however research suggests that possibly this might work:

Instead of

@member.member_addresses.first.address_type = @addr_type

Try

@member.member_addresses.create(:address_type => @addr_type)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜