2010-08-12 115 views
0

我打电话给update_attributes但它似乎并没有改变对象。self.update_attributes不更新属性

def add_to_vote_count(increment) 
    vc = vote_count ? vote_count : 0 
    puts "CALLED a_t_v_c(#{increment}), NEW VOTE COUNT SHOULD BE #{vc + increment}" 
    self.update_attributes(:vote_count => (vc + increment)) 
    end 

下面是一些控制台测试:

ruby-1.8.7-p299 > p = Factory(:playlist) 
=> #<Playlist id: 56, user_id: 0, message: "Lorem ipsum dolor sit amet, consectetur adipisicing...", title: "Ipsum Dolor", flag: 2, created_at: "2010-08-12 18:18:51", updated_at: "2010-08-12 18:18:51", moderation_score: 0, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, category_id: nil, widget_id: 22100001, permalink: #<ActiveSupport::Multibyte::Chars:0x102feba48 @wrapped_string="ipsum-dolor-27">, cached_tag_list: "", vote_count: 0> 

ruby-1.8.7-p299 > p.vote_count 
=> 0 
ruby-1.8.7-p299 > p.votes 
=> [] 
ruby-1.8.7-p299 > p.votes << Vote.new(:vote => true) 
CALLED a_t_v_c(1), NEW VOTE COUNT SHOULD BE 1 


VOTE CREATED 

=> [#<Vote id: 235, vote: true, voteable_id: 56, voteable_type: "Playlist", voter_id: nil, voter_type: nil, created_at: "2010-08-12 18:19:09", updated_at: "2010-08-12 18:19:09">] 

ruby-1.8.7-p299 > p.votes 
=> [#<Vote id: 235, vote: true, voteable_id: 56, voteable_type: "Playlist", voter_id: nil, voter_type: nil, created_at: "2010-08-12 18:19:09", updated_at: "2010-08-12 18:19:09">] 

ruby-1.8.7-p299 > p.vote_count 
=> 0 

ruby-1.8.7-p299 > p 
=> #<Playlist id: 56, user_id: 0, message: "Lorem ipsum dolor sit amet, consectetur adipisicing...", title: "Ipsum Dolor", flag: 2, created_at: "2010-08-12 18:18:51", updated_at: "2010-08-12 18:18:51", moderation_score: 0, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, category_id: nil, widget_id: 22100001, permalink: #<ActiveSupport::Multibyte::Chars:0x102feba48 @wrapped_string="ipsum-dolor-27">, cached_tag_list: "", vote_count: 0> 

任何想法,为什么没有什么是越来越保存?我没有得到任何错误,调用方法通常会返回“正确”的结果。

ruby-1.8.7-p299 > p.update_attributes(:vote_count => 100) 
=> true 
ruby-1.8.7-p299 > p.vote_count 
=> 100 
ruby-1.8.7-p299 > p.add_to_vote_count(10) 
CALLED a_t_v_c(10), NEW VOTE COUNT SHOULD BE 110 
=> true 
ruby-1.8.7-p299 > p.vote_count 
=> 110 

我可以看到的唯一区别是,add_to_vote_count在其after_create方法被调用由Vote类。正如你从输出中看到的,尽管如此,add_to_vote_count肯定会被调用。

#in vote.rb 
    def after_create 
    voteable.add_to_vote_count(vote ? 1 : -1) 
    puts "\n\nVOTE CREATED\n\n" 
    end 

编辑:其实,事实证明,该对象得到更新,但我引用不是。也就是说,p返回没有投票的旧版本,但Playlist.find(p.id)返回正确的一个。我认为这是由于缓存(Rails不希望再次访问数据库中的内存应该有的项目),那么我如何强制Rails意识到这些东西改变了?

+0

好吧,我已根据您的编辑更新了我的答案! – 2010-08-12 19:22:47

回答

1

我真的不知道这是否会帮助你,但你可以使用increment而不是update_attributes

def add_to_vote_count(increment) 
    self.increment!(:vote_count, increment) 
end 

如果你尝试,请让我知道如果这个工作:]

编辑

相信reload方法能够解决您的问题!

+0

这并没有奏效......但是,我想我已经发现了这个问题。它看起来像vote.voteable不同于它附加到的实际播放列表...让我们说,p是一个投票的播放列表,然后p.votes.first.voteable不等于p。奇怪的;我现在需要做一些思考。 – unsorted 2010-08-12 18:47:08

+0

好的,编辑原帖以澄清问题。感谢您的建议; “增量”会做你说过的事情,它看起来比我之前干净一点,所以我会用它。 – unsorted 2010-08-12 18:53:59

+0

'reload'帮助控制台输出。其中一件令人困惑的事情是,我认为我必须在'increment'之后调用'save'?最后,这个问题与'vote_fu'有关,它已经向对象添加了一个名为'vote_count'的属性(我认为),所以出现了名称冲突。重命名为'vote_cache'效果更好。感谢您的建议。 – unsorted 2010-08-12 22:49:16

0

我的猜测是你的对象失败验证,这就是为什么它没有保存。尝试使用感叹号update_attributes!,以便引发错误,并且可以看到最新消息。

祝你好运。

+0

我试过这个并确认验证没问题。这是一个名称空间冲突(请参阅下面的注释)。谢谢你的建议,男人。 – unsorted 2010-08-12 22:49:59

+0

update_attribute跳过验证! – 2013-03-10 12:05:45