views:

47

answers:

1

Hi, I am doing something seemingly pretty easy, but Rails is messing up the SQL. I could just execute my own SQL, but the framework should be able to handle this.

Here is the save I am trying to perform:

w = WhipSenVote.find(:first, :conditions => ["whip_bill_id = ? AND whip_sen_id = ?", bill_id, k])
w.votes_no = w.votes_no - 1
w.save

My generated SQL looks like this:

SELECT * 
FROM "whip_sen_votes" 
WHERE (whip_bill_id = E'1' AND whip_sen_id = 7) 
LIMIT 1

And then:

UPDATE "whip_sen_votes" 
SET "votes_yes" = 14, "updated_at" = '2009-11-13 19:55:54.807000' 
WHERE "id" = 15

The first select statement is correct, but as you can see, the Update SQL statement is pretty wrong, though the votes_yes value is correct.

Any ideas? Thanks!

A: 

It would help to see the WhipSenVote model.

you can also use decrement!

cgr
The model looks like this:class WhipSenVote < ActiveRecord::Base belongs_to :whip_sen named_scope :winners, lambda {|id| {:conditions => ["votes_yes > votes_no AND whip_bill_id = ?", id]}} end
Dan B
Also, not sure I can use decrement, because I need the WHERE clause. Thanks.
Dan B
it would be w.decrement!(:votes_no)
cgr
it is modifying the wrong field? What if you directly set votes_no , does the update behave the same way?
cgr
I think I can see what it is trying to do ... the id in the Update field is the actual id of that row, but it isn't changing the correct row or getting the wrong id. When I use strait SQL and not the framework, it works, which is what I will do until I can figure this out. Strange. I have never had this happen before.....
Dan B
does that -exact- query work? the decimal aspect of the updated_at looks weird.
cgr