2669fbd62908cf7787bd6ac81bad624c

I can't get this to work properly. Prob syntax issue. Help!

1
2
3
4
5
6
7
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 !

Ee0505bbd355292778077fb662c88f13

Fu86

June 20, 2010, June 20, 2010 12:19, permalink

1 rating. Login to rate!

You can simplify the WHERE statement.

Boolean algebra

1
(X AND true) OR (X AND false) == X

SQL

1
2
3
4
5
has_many :fight_wins, :class_name => 'Fight', :finder_sql =>
      '#{sanitize_sql_array(
          "SELECT f.* 
           FROM   fights AS f 
           WHERE  f.challenger_id = ?", id)}'
7855792dbc5f3b4c365344314e2b1ad6

arvanasse

June 21, 2010, June 21, 2010 16:17, permalink

1 rating. Login to rate!

Why not replace #challenger_won with #winner_id?

1
2
3
4
5
6
7
8
9
10
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

Your refactoring





Format Copy from initial code

or Cancel