Has_many and params


#1

Hi, a question about creating (and updating) a model and its children at
the same time. Is it possible with a simple create(params) ?

e.g. a “Post” model which has_many :comments. I’d like to do something
like this:

Post.create { :title => “Forest and Trees”,
:comments => [ { :text=>“Great article” }, { :text => “I don’t get it”
} ] }

I could override ActiveRecord.create and do it manually, but it seems
like something that could be done automagically. However, if I do the
above, I get an error like “Comment expected, got Hash” - it’s like I
need to include an extra value in the hash to tell Rails to create the
child as well, e.g. :id=>0 or :new_record=>false (tried both of those).
So it seems like the only way to do this is manually.


#2

Michael M. schrieb:

I could override ActiveRecord.create and do it manually, but it seems
like something that could be done automagically. However, if I do the
above, I get an error like “Comment expected, got Hash” - it’s like I
need to include an extra value in the hash to tell Rails to create the
child as well, e.g. :id=>0 or :new_record=>false (tried both of those).
So it seems like the only way to do this is manually.

your Post model is expecting Comment objects,

as in

pst = Post.create { :title => “Forest and Trees”}
params[‘comments’].each{|x|
pst << Comment.create(:text => x)
}

pst.save!

something like that should work

/ak


#3

Andy K. wrote:

Michael M. schrieb:

I could override ActiveRecord.create and do it manually, but it seems
like something that could be done automagically. However, if I do the
above, I get an error like “Comment expected, got Hash” - it’s like I
need to include an extra value in the hash to tell Rails to create the
child as well, e.g. :id=>0 or :new_record=>false (tried both of those).
So it seems like the only way to do this is manually.

your Post model is expecting Comment objects,

as in

pst = Post.create { :title => “Forest and Trees”}
params[‘comments’].each{|x|
pst << Comment.create(:text => x)
}

pst.save!

something like that should work

Thanks for the example, but actually I’m trying to initialise the whole
thing from a single nested hash structure, i.e. one which might come
from a client that uploaded an XML document. There are two problems: (a)
Apparently not possible to do it automagically with .create; (b) Worse,
leads to an error due to Comment objects required but receiving a hash
instead.


#4

Michael M. schrieb:

Thanks for the example, but actually I’m trying to initialise the whole
thing from a single nested hash structure, i.e. one which might come
from a client that uploaded an XML document. There are two problems: (a)
Apparently not possible to do it automagically with .create; (b) Worse,
leads to an error due to Comment objects required but receiving a hash
instead.

you could try a custom method in your model, something that gives the
appearance of what you want in the controller

underneath it all, though, you’ll certainly need to create those Comment
records at some point

/ak