I have three models: RaceCards, Races and Wagers.
class RaceCard < ActiveRecord::Base
has_many :races
has_many :wagers
end
class Race < ActiveRecord::Base
belongs_to :race_card
has_many :wagers, :through => :race_card
end
class Wager < ActiveRecord::Base
belongs_to :race_card
has_many :races, :through => :race_card
end
rails g model RaceCard race_card_date:date number_of_race:integer
rails g model Race race_card_id:integer race_nbr:integer
rails g model Wager race_nbr:integer race_card_id:integer wager_type:string payoff:integer
So if I do this in console:
Wager.first.races #All races on the race card are returned. Good!
But I want a way of determining what races are returned so I add a condition:
if I add this: :condition =>{:race_nbr => 1}
Wager.first.races #Return just race 1, but this is static (always set to 1)
My question is how do I set the condition to the race_nbr in the Wager model:
:condition => {:race_nbr => wager.race_nbr} #throws an error
:condition => {:race_nbr => self.race_nbr} #throws an error
I’ve tried a lot of other things and just can’t seem to get it. Any guidance would be appreciated. Thanks in advance.
Update: I’ve now tried the solution suggested by PinnyM below
:condition => "wagers.race_nbr = races.race_nbr" #unfortunately this yields the following:
SQL error or missing database (no such column wagers.race_nbr)
You can do this using scopes: