ruby on rails - Add user id to comment model -
i'm following tutorial :
http://guides.rubyonrails.org/getting_started.html#adding-a-second-model
it works when using commenter , comment user can add name , message want associate comment user id (i have users)
it uses rails generate model comment commenter:string body:text post:references want replace commenter:string user id association (user_id:integer?). in previous question suggested author_id:integer did not work. not sure start , there doesn't seem tutorials on subject (i have read ror guides on associations etc can't find correct way generate user id comment model)
comments_controller.rb
def create @listing = listing.find(params[:listing_id]) @comment = @listing.comments.create(params[:comment]) redirect_to listing_path(@listing) end
you can generate comment mode this:
rails generate model comment user:references body:text post:references
the references type specify create user_id:integer column , adds belongs_to association comment model:
class comment < activerecord::base belongs_to :user belongs_to :post end if want have comment#commenter association refer user rather comment#user, can define in comment model follows:
class comment < activerecord::base belongs_to :commenter, class_name: 'user', foreign_key: 'user_id' belongs_to :post end
Comments
Post a Comment