Join The East Bay Ruby Meetup Group

You'll get invited to our Meetups as soon as they're scheduled!

The East Bay Ruby Meetup Group Message Board › Extending Parent to multiple Children in extended "Rails 2.3: Associate

Extending Parent to multiple Children in extended "Rails 2.3: Associated models accepts_nested_attributes_for" discussion

Seth Galvarro
Posted Jul 31, 2009 8:11 PM
user 9990114
El Cerrito, CA
Post #: 1
Send an Email Post a Greeting
Rookie question (literally - been learning rails for about a month):

So I attended the "New in Rails 2.3: Associated models accepts_nested_attributes_for" meeting and am now trying to get multiple children onto a form to be associated to the parent that is also on the form.

I found an example from Rails Forum to do this at http://railsforum.com..., but I can't seem to get it to work in the context of the Rails 2.3. This posting is from 2006, which could mean it's archaic, but I don't know enough to know. Using the terminology from this example, my projects and tasks are in separate models

At the step in the example:
# in projects_controller.rb
def new
@project = Project.new
5.times { @project.tasks.build }
end

I've added the 5.times statement in my controller, but now I toggle back and forth between 2 errors. I either get a "NameError ProjectsController, uninitialized constant Project::task" or a "No Method Error in Controller... undefined method 'child'" I've been going between using plural (i.e. with 's' at the end) for the child's name & singular for the child's name.

What am I missing? What information do I need to post to make this clear? Any help will be helpful.
Wolfram Arnold
Posted Aug 30, 2009 8:27 PM
wolframarnold
San Francisco, CA
Post #: 7
Send an Email You are unable to greet this member
Seth,

I can't tell what the problem is from that slim piece of code; the first error would seem to indicate your model is missing the has_many :tasks declaration, the second is less clear because you're not showing the view code.

I personally found the Rails's own test suite most helpful in understanding this feature. One thing you can always be sure of when documentation is failing you is to look at the tests that actually exercise the code. The Active Records tests are pretty well structured. The examples I used in my presentation borrow from them...

Find your Active Record gem install directory and browse to the test folder and look inside "cases". To find the install directory, you can use this:

gem list -d activerecord

Best,

Wolf
Powered by mvnForum