I'm not sure if this is feasible or even preferable
But I'd like to build a has_many relationship or a conditional named_scope type relationship to ease my pain into one simple relationship instead of two.
Currently I have two models which basically contain the same information and belong to the same Model rooms thru the same key room_id, but hold data in two different ways. One contains specific information for a particular date and the second generic rates for days of the week. The main obstacle is that Availables may contain no information for a particular record at all, in which case it needs to defer to room_rates for average rather than specific prices for a given date.
Any pointers or suggestions? Thanks
Availables
+--------+-------+-------+------------+---------+-----+
| id | price | spots | bookdate | room_id | min |
+--------+-------+-------+------------+---------+-----+
| 180315 | 14.4 | 1 | 2010-02-27 | 2517 | 1 |
| 231726 | 15.84 | 1 | 2010-03-24 | 2517 | 1 |
| 180311 | 14.4 | 1 | 2010-02-23 | 2517 | 1 |
| 180313 | 14.4 | 1 | 2010-02-25 | 2517 | 1 |
+--------+-------+-------+------------+---------+-----+
Room_rates
+-------+---------+-----------+-------+-----+
| id | room_id | dayofweek | price | min |
+-------+---------+-----------+-------+-----+
| 87936 | 2517 | 0 | 14.58 | 1 |
| 87937 | 2517 | 1 | 14.58 | 1 |
| 87938 | 2517 | 2 | 14.52 | 1 |
| 87939 | 2517 | 3 | 14.52 | 1 |
| 87940 | 2517 | 4 | 14.52 | 1 |
| 87941 | 2517 | 5 | 14.4 | 1 |
| 87942 | 2517 | 6 | 14.63 | 1 |
+-------+---------+-----------+-------+-----+
Or possibly a finder_sql method? Such as:
has_many :aggregates,
:class_name => 'Available',
:finder_sql => 'SELECT room_id, price, spots, bookdate, MIN(source)
FROM availables
WHERE room_id = #{id}
GROUP BY room_id, price, spots, bookdate'
But it also needs to fill in missing records for bookdates that don't exist by joining room_rates