2

Is there any way by which i can create an object of a model that belongs to two other models for example if i have users who have many posts and then posts which can have many comments and comments belongs to both user and post. if i do post.comments.create() it will associate only post with comment and if i do user.comments.create() then it will associate user. If i want to associate both with comments then what's the method for that. I know i can use polymorphic association but is there any other way too?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mandeep
  • 9,093
  • 2
  • 26
  • 36
  • possible duplicate of http://stackoverflow.com/questions/3911890/rails-belongs-to-many-models – Stefan Lyew Sep 24 '13 at 05:39
  • @StefanLyew it only talks about how to handle associations. It doesn't say anything about how to create a comments object – Mandeep Sep 24 '13 at 05:44

3 Answers3

3

First things first, when you are talking about associations, you must keep in mind that we build not create. A very simple way to do what you need is do

    class Comment < ActiveRecord::Base
      belongs_to :user
   end

and dont forget to add the other side of the relation in User where:

 class User < ActiveRecord::Base
  has_many :comments
 end

Now, I understand that you must have created a field user_id in comments table. If not, you need to add it by this migration.

rails g migration add_user_id_to_comments user_id:string

now do a rake db:migrate

alternatively and a more better method will be .

while creating the model comments you add users:references in the migration line like this

rails g model Comment text:string post:references user:references

in this way , one side of the relation will automatically be added to the model and also the user_id and post_id fields will be added automatically to your comments table.

Coming back to your question. pass user id in a hidden field if you find no other way like this:

<%= hidden_field_tag "userid", current_user.id%>

I expect thet you have current user defined. now you can accept this in the controller of the comments like this

If params[:userid]
 user_id = params[:userid]
end

you can include this just before the save function in the create action of comments controller.

Hope this helps

Cheers!

aelor
  • 10,892
  • 3
  • 32
  • 48
  • Aha i knew most of the part except for the hidden_field_tag and i think thats the way to go..Thanks – Mandeep Sep 24 '13 at 05:48
2

You can use belongs_to with both models. The only difference is that while creating a comment you'll have to explicitly mention the id of the model you're not creating through. I'll give an example:

class Comment
  belongs_to :user
  belongs_to :post
end

comment = post.comments.create(user_id: some_user_id)

Since I created the comment via the post comments relation the post id will automatically get inserted to the comment's post_id attribute. I specifically mentioned the user_id so that comment.user will return the user which has an id of some_user_id.

EDIT

When creating the comment, to use the comments attributes in the params hash use the following:

comment = post.comments.build(params[:comment])
comment.user_id = some_user_id
comment.save
Erez Rabih
  • 15,562
  • 3
  • 47
  • 64
  • I know i can do that but what if the comment attributes are coming from a form? – Mandeep Sep 24 '13 at 05:42
  • i think it's better to use a hidden_field_tag in form and specify user_id there rather that do this..but thanks appreciate your help :) – Mandeep Sep 24 '13 at 05:52
0

It might be even more intuitive to create the Comment like this:

comment = Comment.create(user_id: user-you-want-to-associate.id, post_id: post-you-want-to-associate.id)

Jillian Hoenig
  • 137
  • 1
  • 6
  • 28
  • It's too verbose though :)..i think more rails way would be to setup proper nested routes and then pass in post_id from the params hash, like: current_user.comments.create(post_id: post_id_associated_coming_from_params) – Mandeep Aug 18 '15 at 04:05