On 21 July 2013 18:28, James Gray <zaa...@gmail.com> wrote: > Hi all, > > In my controller I am doing the following to populate a nested form for a > has_many through association: > > def new > @specification = Specification.new > > Component.find_each.each do |component| > @specification.component_specifications.build(:component_id => > component.id) > end > > The idea being whenever someone creates or edits a form, it will be > populated with all components at that time so that when they save those > components that specification will be associated with the newly created > specification. The problem I am having is I can't work out how to pass > component name to display in my form for the as yet nonexistent > component_specification as it is not accessible through the > ComponentSpecification model. > > My models: > > class Specification < ActiveRecord::Base > attr_accessible :description, :name, :component_specifications_attributes > > validates :name, :presence => true, :uniqueness => true > > has_many :component_specifications > has_many :components, :through => :component_specifications > > accepts_nested_attributes_for :component_specifications, :allow_destroy => > true > end > > > class ComponentSpecification < ActiveRecord::Base > attr_accessible :note, :colour, :brand, :components > > has_many :components
I think that should be belongs_to :component, unless my brain is fading again. > > belongs_to :specification > end > > class Component < ActiveRecord::Base > attr_accessible :description, :name > > belongs_to :component_specifications and that should be has_many Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLu%3DXQCmYTZgyCnJEahq5%2BwKtXwxLy6%2B2stP6Fmg1dfkBA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.