active record does use all the passed in fields in new/create
I have a website where people can comment and each comment has a category they can pick from the a drop down menue. In my comment modal I have
belongs_to :category
and I have a category_id in the comment table.
when a user submit the comments in the params I get params[:comment] = {"locale"=>"en", "body"=>"fds", "category_id" =>2, "from_identifier"=>2130706433, "from_type"=>"ip", "cookie_user_token"=>"130784267178572", "user_id"=>3}
which is exactly what I want. However when I do
Comment.create(params[:comment])
I get
#<Comment id: nil, from_type: "ip", from_identifier: 2130706433, cookie_user_token: 130784267178572, body: "fds", locale: "en", positive_vote_count: 0, adjusted_positive_vote_count: 0.0, negative_vote_count: 0, adjusted_negative_vote_count: 0.0, flag_vote_count: 0, adjusted_flag_vote_count: 0.0, impression_count: 0, visit_count: 0, rank: 137.0, created_at: nil, updated_at: nil, user_id: 3, category_id: nil>
as you can see category_id is nil.
User model has the same relationship with active record, so I don't know why that gets saved and category_id doesn't开发者_运维技巧.
Right now I do
comm = Comment.create(params[:comment])
comm.category_id = params[:comment][:category_id]
Any idea why, and what I should do to avoid the hack up there?
Have you used attr_protected
or attr_accessible
to prevent mass assignment to category_id
?
If so, that's the problem.
精彩评论