Help with nested model forms

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?

Thanks,

Elías

Hello Elias

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).

Regards, T.

On Mon, Jan 18, 2010 at 12:09 AM, elioncho [email protected] wrote:

Elías, I would recommend taking a look at the following screencasts:

http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

Good luck,

-Conrad

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs