views:

12

answers:

1

I have a model defined as

class A < ActiveRecord::Base
  belongs_to :b
  belongs_to :c
end

How do I create a new instance of A associated with both b and c. I've got the ids for b and c.

+1  A: 

You can create a new instance of a like this:

A.create :b => b, :c => c
jigfox
Yes, this works! But there's another problem. This is inside a typical "create" action handling a POST to create a new A.Now, A.create gives an instance with b and c set but the other fields are not set. I presume I could pass them as attributes as well, but I don't want to do that. Shouldn't rails automatically do that?
manu1001
Ok, just found out I can pass a hash of params to create. This is what I wanted to avoid having to code in the model properties here.
manu1001