Hi guys, I have a find statement written like so in my event model: @events = find(:all, :conditions => ["((? BETWEEN start_on AND end_on) OR (? BETWEEN start_on AND end_on)) AND id != ?", start_time,end_time,event_id]) but I would like to add to this, to only return the results where the event id and (an additional) user id appear in another link-table called subscriptions. My event model has the following defined at the top: class Event < ActiveRecord::Base has_many :subscriptions, :dependent => :destroy has_many :users, :through => :subscriptions and likewise the subscription model has the following defined at the top: class Subscription < ActiveRecord::Base belongs_to :events belongs_to :users I could probably hard code it into the :conditions in the above find method call above but this seems wrong - any ideas? Cheers Mick -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I may be missing something but have you tried: Event.find(:all, :conditions => [ ... ], :include => :users) I am unsure whether this would work with a has_many :through but it is worth a shot. At the very least you should be able to :include => :subscriptions and collect your users from that. -Shawn On 3/19/07, Mick <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi guys, I have a find statement written like so in my event model: > > @events = find(:all, :conditions => ["((? BETWEEN start_on AND end_on) > OR (? BETWEEN start_on AND end_on)) AND id != ?", > start_time,end_time,event_id]) > > but I would like to add to this, to only return the results where the > event id and (an additional) user id appear in another link-table called > subscriptions. > > My event model has the following defined at the top: > > class Event < ActiveRecord::Base > has_many :subscriptions, :dependent => :destroy > has_many :users, :through => :subscriptions > > and likewise the subscription model has the following defined at the > top: > > class Subscription < ActiveRecord::Base > belongs_to :events > belongs_to :users > > I could probably hard code it into the :conditions in the above find > method call above but this seems wrong - any ideas? > > Cheers > Mick > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shawn Roske wrote:> I may be missing something but have you tried: > > Event.find(:all, :conditions => [ ... ], :include => :users) > > I am unsure whether this would work with a has_many :through but it is > worth a shot. At the very least you should be able to :include => > :subscriptions and collect your users from that. > > -ShawnHmmm, I tried that and it doesnt work. The functionality I am trying to creat is to display a list of a user''s subscriptions and check against any other event to see if there are 2 events which clash (time-wise). But currently it returns all events which clash, regardless of whether the user has subscribed (i.e. has a record stored in the subscriptions table). -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, I see, looking at http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000992 :include gives a LEFT OUTER JOIN which you don''t want, you should be able to use :join to do this, e.g.: Event.find(:all, :conditions => [ ... ], join => "INNER JOIN subscriptions ON subscriptions.event_id = id") There may be a nicer rails-ish way of doing this, but I am unaware of it. -Shawn On 3/19/07, Mick <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Shawn Roske wrote: > > I may be missing something but have you tried: > > > > Event.find(:all, :conditions => [ ... ], :include => :users) > > > > I am unsure whether this would work with a has_many :through but it is > > worth a shot. At the very least you should be able to :include => > > :subscriptions and collect your users from that. > > > > -Shawn > > Hmmm, I tried that and it doesnt work. The functionality I am trying to > creat is to display a list of a user''s subscriptions and check against > any other event to see if there are 2 events which clash (time-wise). > > But currently it returns all events which clash, regardless of whether > the user has subscribed (i.e. has a record stored in the subscriptions > table). > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---