views:

34

answers:

2

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?

+1  A: 

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!

j.
That did not work... however, I think I have uncovered the problem. It looks like vote.voteable is different from the actual playlist it's getting appended to...let's say p is a playlist with one vote, then p.votes.first.voteable is not equal to p. Weird; I need to do some thinking now.
unsorted
Okay, edited the original post to clarify the problem. Thanks for the suggestion; `increment` does what you said it would do, and it seems a little cleaner than what I had before, so I'll use that.
unsorted
`reload` helped the console output. one of the things that was confusing things was that I think I had to call `save` after `increment`? In the end, the problem had something to do with `vote_fu` already adding an attribute called `vote_count` (I think) to the object, so there was a name collision. Renaming to `vote_cache` worked much better. Thanks for the suggestions.
unsorted
You're welcome!
j.
A: 

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.

Jonathan
I tried this and confirmed that validation was okay. It was a namespace conflict (see comment below). Thanks for the suggestion, man.
unsorted