class Message < ActiveRecord::Base
belongs_to :author
accepts_nested_attributes_for :author
end
I'm using the above and it's working perfectly. The message gets saved
with an author_id attribute which references the author (tha author
record gets saved too).
The problem:
Sometimes I don't want to save the author because the author already
exists (I just want to reference it). I just want the message to get
saved with the author_id reference. How can I override or modify the
normal saving behaviour when using the accepts_nested_attributes_for
method?
I wonder that this works in any way. Normally in an 1:n relation the 1
accepts the nested attributes for the n. So this would be the way to go
for you:
class Author < ActiveRecord::Base
has_many :messages
accepts_nested_attributes_for :messages
end
Then you can make a (possibly almost empty) form for Author with
fields_for the message(s).