0

I have three models:

class User < ActiveRecord::Base has_many :basiccases end class Basiccase < ActiveRecord::Base belongs_to :user belongs_to :form3_c end class Form3C < ActiveRecord::Base has_one :basiccases end 

How can I add a new Form3C to the Basiccase? Do I need to use a Basiccase controller or Form3C controller to create a form? If I use Form3cController how can I set the foreign key in the BasiccaseController?

1
  • The has_one relation should go into a model, not a controller. So if you have a Form3C model class, try to add there the relation. The has_one relation should go to singular as in has_one :basiccase. You would normally build the Basiccase from the Form3C instance: self.build_basiccase will build the basiccase that belongs_to Form3C. Commented Mar 24, 2012 at 17:52

2 Answers 2

4

Relationships like has_one are defined on models rather than controllers. I think you want this (to be able to add a Form3c to a Basiccase--not sure which direction you want the relationship to go):

class Form3c < ActiveRecord::Base belongs_to :basiccase end class Basiccase < ActiveRecord::Base belongs_to :user has_one :form3c end 

For tips on building the controller and form (view), look at this Rails tutorial, starting around section 6.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm really confused where to keep the has_one relation as both are one to one. My requirement is user should be able to add new form to a basic case.Can you suggest me how I can do this ?
0

Though your naming of model is not that convincing, I am assuming your basiccases table has one column form3c_id.

You need to create a form for form3c object then in controller you can build a basiccase object like @form3c_build_basiccase which will automatically take form3c_id as a foreign key. Refer to this rails cast.

Or you may go for form3c model accepts_nested_attributes_for :basiccase. See about it here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.