I'm trying to reduce the number of queries in my application and need some help with the following setup:
I have 5 models:
- Bet
- Choice
- Spotprice
- Spotarea
- Product
They are associated with the following:
- Bet belongs_to Choice
- Choice belongs_to Spotarea
- Choice belongs_to Product
- Choice has_many Bets
- Spotprice belongs_to Spotarea
- Spotprice belongs_to Product
- Spotarea has_many Spotprices
- Spotarea has_many Choices
- Product has_many Sprotprices
- Product has_many Choices
My goal is to find the Spotprices that matches a Specific Bet. To do that I uses the following queries, but I'm sure it can be done in a better way, so when I run through 100 bets and want to see if they are above or below the corrosponding Spotprice I don't overload the DB with queries.
a = Bet.find(5)
b = Choice.find(a.choice_id)
c = Spotprice.where(:spotarea_id => b.spotarea_id, :product_id => b.product_id,
:deliverydate => b.deliverydate).first
Thanks!