Rails attributes not being saved -
when trying save new entity (vote
) ajax calls, attributes not being assigned.
class , method being used:
class votecontroller < applicationcontroller respond_to :json def vote question_id = params[:question][:id] user_id = current_user.id vote = vote.where(["question_id = :q", { q: question_id }]).where(["user_id = :u", { u: user_id }]).take respond_with |format| if vote.nil? @vote = vote.new @vote.question = question_id @vote.user = user_id @vote.save format.json { render :json => { :status => 'ok' } } else format.json { render :json => { :status => 'failed', :msg => 'you voted' } } end end end end
the model:
class vote < activerecord::base belongs_to :user belongs_to :question attr_accessor :user, :question end
and migration:
class createvotes < activerecord::migration def change create_table :votes |t| t.references :question t.references :user end end end
these parameters being sent:
parameters: {"utf8"=>"✓", "question"=>{"id"=>"1"}, "commit"=>"up vote"}
with puts
tested current_user.id
indeed returns value.
and here query executed:
insert `votes` values ()
my records in database:
+----+-------------+---------+ | id | question_id | user_id | +----+-------------+---------+ | 1 | null | null | | 2 | null | null | | 3 | null | null | | 4 | null | null | | 5 | null | null | | 6 | null | null | | 7 | null | null | +----+-------------+---------+
why aren't these attributes being assigned?
because attr_accessor :user, :question
"covering up" activerecord's methods of same name. remove line , try again.
Comments
Post a Comment