Putting your head on it for a long time. On Rails 2.3.2, Ruby 1.9.1.
An attempt to use one form to create three objects that have these relationships:
class Person has_one :goat end class Goat belongs_to :person has_many :kids end class Goat::Kid belongs_to :goat end
Here is a brief description of the circuit:
Person first_name last_name Goat name color Goat::Kid nickname age
I would like my #create action #create create new instances of all three models with the specified associations. However, although it seems that the my params hash is passed to the controller as it should (based on the backtrace logs in the browser when it explodes), the Goat::Kid object does not collect the parameters.
irb (an irb session is just a pseudo-representation of what I'm trying to accomplish, so if it doesn’t call #save! or any other things, it really doesn’t mean that it’s correct. I am trying to do this through a browser / web form.)
a = Person.new :first_name => 'Leopold', :last_name => 'Bloom' b = Goat.new :name => 'Billy', :color => 'white' c = Goat::Kid.new :nickname => 'Jr.', :age => 2 a.goat.kids >> []
Now I can’t understand how to get a view, to pass parameters to each object and force the controller to save these parameters in db.
My questions are: A) is this a good place to use nested_attributes_for , and if so, how do I declare this with a namespace? B) Is there a much simpler and clearer way to do this?
Switching parameters to the three models was very difficult for me, and no matter how many documents I read, I can’t circle my head around it ( #form_for and #fields_for ). The namespace further complicates this. Thanks for any help!
Addendum: if I declare
accepts_nested_attributes_for
What is the correct way to use a character argument for a model with names?
accepts_nested_attributes_for :kids, :through => :goats
or
accepts_nested_attributes_for :goats_kids, :through => :goats
or
accepts_nested_attributes_for :goats::kids, :through => :goats
I'm not sure how models with names translate into their character identifiers. Thanks!