开发者

confused about calling `self` in a finder

I have a model Vote, and I'm using an after_validation callback.

class Vote < ActiveRecord::Base

  after_validation :destroy_reciprocal_votes

  belongs开发者_StackOverflow中文版_to :voteable, :polymorphic => true
  belongs_to :voter, :polymorphic => true

  private

  def destroy_reciprocal_votes
    votes = Vote.delete_all(:vote => false, :voteable_type => "Recipe", :voteable_id => self.voteable_id, :voter_type => "User", :voter_id => self.voter_id)
  end
end

You can see that in my method destroy_reciprocal_votes, I called self.voteable_id and self.voter_id, why does this return nil? and how should I retrieve my id's here?

When I do it in console it works fine:

>> voteable_id = "3"
>> voter_id = "162"
>> Vote.delete_all(:vote => false, :voteable_type => "Recipe", :voteable_id => voteable_id, :voter_type => "User", :voter_id => voter_id)


You don't have to refer to id as long as you specify an association. Try this:

Vote.delete_all(:vote => false, :voteable_type => "Recipe", :voteable_id => voteable, :voter_type => "User", :voter_id => voter)

Update

Changed to :voteable_id => voteable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜