x = Person.create(:phonebook => pb, …)
does not work is that the hash params for create have to be actual
attributes of the model, and not associations?
It will work. You can do it any one of the ways that were mentioned.
Hmm, my test in script/console has it not working unless I specify the
‘id’, that is, unless I do this:
specify id explicitly:
x = Person.create(:phonebook_id => pb.id)
x.save # works
but
rely on association:
x = Person.create(:phonebook => pb, …)
x.save # fails with a validation error: phonebook cannot be blank
is something more/or else messed up?
Here’s a guess, after relooking over your code above. The problem could naming conventions.
When you say belongs_to :phonebook, Rails expects to look for a class named Phonebook (lowercase ‘b’), not PhoneBook
If your model is named PhoneBook, the association should be: belongs_to :phone_book
You can override the default behavior with something like:
belongs_to :phonebook, :class_name => ‘PhoneBook’
but it’s usually easier to do things the way Rails expects them to be done, unless there is an important reason to use different naming conventions.
Unless your model is not really PhoneBook…? the point is, the difference between PhoneBook and Phonebook, and phonebook or phone_book, is actually significant, and will cause you bugs and headaches if you don’t use the one rails will use by default.