I have 3 models named Article, User, Comment
class Article < ActiveRecord::Base
has_many :comments
end
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :article
end
Now, if I want to build a comment for an article, I can use
@comment = @article.comments.build(comment_params)
this will add article_id to the comment object
Now, if I want to add the user_id to object, I can add in the following way
@comment.user_id = current_user.id
But, if I want to auto populate user_id like article_id, what way I can do?
Aucun commentaire:
Enregistrer un commentaire