Tom Tom
2012-Mar-28 18:13 UTC
How does a has_many :through association and its join model attribute interact?
I am having real difficulty getting my head around the issue of setting this attribute, units_alloc, in my join model? My models: class Contract < AR::Base has_many :codelines has_many :codes, :through => :codelines accepts_nested_attributes_for :codes attr_accessible :codes_attributes, :codes; :authnum,:st_date,:end_date end class Codeline < AR::Base belongs_to :contract belongs_to :code units_alloc ...... this is the attribute I would like to set end class Code < AR::Base has_many :codelines has_many :contracts, :through => :codelines end The new action of my app/controllers/contracts_controller.rb def new @contract = Contract.new @contract.codes.build end the partial for my view in app/views/contracts/_fields.html.haml = f.fields_for :codes do |ff| .field = ff.label :name, "Code Name" %br/ = ff.text_field :code_name .field . . = f.fields_for :codelines do |ff| .field = ff.label :name, "Units Alloc" %br/ = ff.text_field :units_alloc From reading Rails Guides and watching railscasts #196 and #197 and researching nested attributes on the internet I came to the understanding that the @contract.codes.build line in the new action of my app/controllers/contracts_controller.rb not only built the code object but that it also built the codelines object. If I leave my new action of my app/controllers/contracts_controller.rb as above that is exactly what happens. My codelines table is populated as follows: id contract_id code_id units_alloc 1 1 1 .... @contract.codes.build But if you look at my view I would actually like to set units_alloc and this line @contract.codes.build does not give me access to units_alloc since it is in the codeline model. I added @contract.codelines.build to the new action of my app/controllers/contracts_controller.rb and now the units_alloc shows in my view and I am able to set it. There is a problem though,I now have two rows in my codelines table. One resulting from the @contract.codes.build where both the code object and codeline object are built and the second row is as a result of the @contract.codelines.build. Here is what my codelines table shows: id contract_id code_id units_alloc 1 1 1 ..@contract.codes.build 2 1 80.00 ..@contract.codelines.build Should''nt I be able to get access to units_alloc through the first build given by @contract.codes.build, where both the code object and the codeline object is built? Does anyone know if my understanding of this issue is correct or could you point me to a resource where all will be revealed? Thanks for any suggestions. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.