I was thinking you could create an 'available' scope, and use that to encapsulate your requirements, but I guess I still don't know how you would simplify your query. Maybe do it in two queries, start with a select id from reservations, then pass that into your spaces query as a parameter rather than a subquery.
space_ids = Reservation.where('enddate > ? and startdate > ?', start_dt, end_dt).select(:space_id)
all_spaces = Spaces.where('id not in ?', space_ids)
Walter
On Apr 27, 2015, at 1:40 PM, Norm Scherer <normscherer@earthlink.net> wrote:
> It does have named scopes but I am not sure what that would bring to the table. How would one do it using named scopes?
>
> Norm
>
> On 04/25/2015 01:52 PM, Walter Lee Davis wrote:
>> I can't recall, does Rails 2.3 have named scopes? You may be able to do this with one of those. It's been quite a while since I worked in 2.3.
>>
>> Walter
>>
>> On Apr 25, 2015, at 3:06 PM, Norm Scherer <normscherer@earthlink.net> wrote:
>>
>>> I have an app running Rails 2.3 with Ruby 1.8.7. Upgrading is not in the cards for a while. I am trying to make it database agnostic so I want to eliminate the use of find_by_sql. I have got it down to only one find left but I have not been able to figure out how to do this by a find.
>>>
>>> I have two tables:
>>> Reservation belongs_to :space
>>> int space_id
>>> date startdate
>>> date enddate
>>> and a bunch of other stuff
>>>
>>> Space - has many reservations
>>> containing a bunch of stuff about spaces
>>>
>>> I want to fetch the spaces which are not used by a reservation meeting certain conditions.
>>> The current find is:
>>> all_spaces = find_by_sql("SELECT * FROM spaces
>>> WHERE id NOT IN (SELECT space_id FROM reservations
>>> WHERE enddate > \'#{start_dt}\'
>>> AND startdate < \'#{end_dt}\')
>>> ")
>>> Is there a way I can just use a Space.all ...?
>>>
>>> Norm
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
>>> To post to this group, send email to rubyonrails-talk@googlegroups.com.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/553BE59C.9040001%40earthlink.net.
>>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/553E747A.20505%40earthlink.net.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/E30D055C-8E0D-42F8-AA68-D215AF07EA05%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment