self.update_attributes not updating the attribute
I'm calling update_attributes
but it doesn't seem to be changing the object.
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
Here's some console testing:
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>
Any idea why nothing is getting saved? I don't get any errors, and calling the methods normally returns the 'correct' results.
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
The only difference I can see is that add_to_vote_count
is being called by the Vote
class in its after_create
method. As you can see from the output, though, add_to_vote_count
is definitely getting called.
#in vote.rb
def after_create
voteable.add_to_vote_count( vote ? 1 : -1 )
puts "\n\nVOTE CREATED\n\n"
end
Edit: Actually, it turns out that the object is getting updated, but my reference is not. That is to say, p
returns the old version with no votes, but Playlist.find(p.id)
returns the correct one. I assume开发者_高级运维 this is due to caching (Rails doesn't want to hit the database again for an item it should have in memory), so how do I force Rails to realize that stuff changed?
I really don't know if this is going to help you, but you can use increment
instead of update_attributes
.
def add_to_vote_count(increment)
self.increment!(:vote_count, increment)
end
If you try, please let me know if this worked :]
Edit
I believe the reload
method can solve your problem!
My guess is that your object is failing validation and that's why its not saving. Try using update_attributes!
with the exclamation so it throws and error and you can see whats up.
Good luck.
精彩评论