has_many :fight_wins, :class_name => 'Fight', :finder_sql =>
'#{sanitize_sql_array(
"SELECT f.*
FROM fights AS f
WHERE (f.challenger_id = ? AND f.challenger_won = ?) OR
(f.challengee_id = ? AND f.challenger_won = ?)
", id, true, id, false)}'
Refactorings
No refactoring yet !
Fu86
June 20, 2010, June 20, 2010 12:19, permalink
You can simplify the WHERE statement.
(X AND true) OR (X AND false) == X
has_many :fight_wins, :class_name => 'Fight', :finder_sql =>
'#{sanitize_sql_array(
"SELECT f.*
FROM fights AS f
WHERE f.challenger_id = ?", id)}'
arvanasse
June 21, 2010, June 21, 2010 16:17, permalink
Why not replace #challenger_won with #winner_id?
Challenger < ActiveRecord::Base
has_many :challenges, :class_name => 'Fight', :foreign_key => :challenger_id
has_many :defenses, :class_name => 'Fight', :foreign_key => :challengee_id
has_many :fight_wins, :class_name => 'Fight', :foreign_key => :winner_id
def challenger_won
self.winner_id == self.challenger_id
end
end
I can't get this to work properly. Prob syntax issue. Help!