Forum: Ruby on Rails has_many and params

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cabf735ce7b8b4471ef46ea54f71832d?d=identicon&s=25 Michael Mahemoff (Guest)
on 2007-07-06 21:29
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.
10a8337cf8879d37affd54e1cab4d79e?d=identicon&s=25 Andy Koch (Guest)
on 2007-07-06 23:05
(Received via mailing list)
Michael Mahemoff 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
Cabf735ce7b8b4471ef46ea54f71832d?d=identicon&s=25 Michael Mahemoff (Guest)
on 2007-07-07 00:23
Andy Koch wrote:
> Michael Mahemoff 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.
10a8337cf8879d37affd54e1cab4d79e?d=identicon&s=25 Andy Koch (Guest)
on 2007-07-07 00:42
(Received via mailing list)
Michael Mahemoff 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
This topic is locked and can not be replied to.