Hi I''m trying to perform a simple find on a class but am having trouble implementing it. What I want to perform a dynamic find for each different user. The user''s location would be variable and I want to display all the relevant venues for that user. My code currently looks something like this: @location = @current_user.location @nearby_venues = Venue.find(:all, :conditions => location_id @location, :order => ''name'') This does not work however. I''ve tried changing the conditions statement around a bit but with no luck. If anybody could tell me how to perform this find, it''d really help me out. Thanks in advance. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 24, 4:53 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > I''m trying to perform a simple find on a class but am having trouble > implementing it. > > What I want to perform a dynamic find for each different user. The > user''s location would be variable and I want to display all the > relevant venues for that user. > > My code currently looks something like this: > > @location = @current_user.location > > @nearby_venues = Venue.find(:all, :conditions => location_id > @location, :order => ''name'') > > This does not work however. I''ve tried changing the conditions > statement around a bit but with no luck. > > If anybody could tell me how to perform this find, it''d really help me > out. > > Thanks in advance.You need to correct your :conditions :conditions => {:location_id => @location.id} --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@nearby_venues = Venue.find(:all, :conditions => {:location_id >@location.id}, :order => ''name'') should work in that case --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Cheers, that worked perfectly. What I''m trying to do now is to list the events which belong to these "nearby_venues" and which occur after todays date. I have no problem in getting the events from the nearby venues but am having problems when trying to couple this with the date condiiton. My current code looks like this: @today = Date.today @upcoming_events = Event.find(:all, :conditions => {"venue_id = ? and date > ?", @nearby_venues, @today}, order => date) This throws up an error since theres more than one value in @nearby_venues so I try using the solution given above, and it looks as follows: @upcoming_events = Event.find(:all, :conditions => {:venue_id => @nearby_venues and :date > @today}, order => date) This doesn''t work either however, any advice? On 24 Sep, 10:06, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 24, 4:53 pm, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi > > > I''m trying to perform a simple find on a class but am having trouble > > implementing it. > > > What I want to perform a dynamic find for each different user. The > > user''s location would be variable and I want to display all the > > relevant venues for that user. > > > My code currently looks something like this: > > > @location = @current_user.location > > > @nearby_venues = Venue.find(:all, :conditions => location_id > > @location, :order => ''name'') > > > This does not work however. I''ve tried changing the conditions > > statement around a bit but with no luck. > > > If anybody could tell me how to perform this find, it''d really help me > > out. > > > Thanks in advance. > > You need to correct your :conditions > > :conditions => {:location_id => @location.id}--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 25, 9:20 am, Gearóid O''Ceallaigh <gearoi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Cheers, that worked perfectly. > > > @upcoming_events = Event.find(:all, :conditions => {:venue_id => > @nearby_venues and :date > @today}, order => date) > > This doesn''t work either however, any advice?Oops, that doesn''t even look like syntatically correct ruby. You can''t use the ever so trendy hash form of conditions for this, ie you need to use the more boring sql fragment form. There are examples in the api docs for ActiveRecord. Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
several details wrong here :) - :conditions => {...} is defining a hash. You can''t use "and" within that, only key/value pairs. So you must use one of the alternative syntax variants: @upcoming_events = Event.find(:all, :conditions => ["venue_id IN (?) AND date > ''?'' ", @nearby_venues.join(","), Time.now.to_s(:db) ], order => date) so using a kind of SQL string, the variable part replaced by question marks and the values to be filled in for the ? directly following the string --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 25, 9:40 am, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> > @upcoming_events = Event.find(:all, :conditions => ["venue_id IN (?) > AND date > ''?'' ", @nearby_venues.join(","), Time.now.to_s(:db) ], > order => date)A minor tidyup: the above is equivalent to @upcoming_events = Event.find(:all, :conditions => ["venue_id IN (?) AND date > ? ", @nearby_venues, Time.now ], order => ''date'') Active Record takes care of the boring stuff like creating the comma separated list of ids and so on. Fred> > so using a kind of SQL string, the variable part replaced by question > marks > and the values to be filled in for the ? directly following the string--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 25, 4:44 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 25, 9:40 am, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote: > @upcoming_events = Event.find(:all, :conditions => ["venue_id IN (?) > AND date > ? ", @nearby_venues, Time.now ], order => ''date'') > > Active Record takes care of the boring stuff like creating the comma > separated list of ids and so on.This is what I like so much about AR. I think Date.today might be more logically correct than using Time.now, though. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I think Date.today might be more logically correct than using > Time.now, though.Depends on some details. the results: Date.today: "2008-09-25" Time.now: "2008-09-25 11:56:21" So Date.today would get you all records of today, including those from the morning to now. Time.now includes the time, ignoring the records at morning. Somehow Time.now is a bit more often seen, but that may have any other reason. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 25, 6:01 pm, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> Depends on some details. the results: > > Date.today: "2008-09-25" > Time.now: "2008-09-25 11:56:21" > > So Date.today would get you all records of today, including those from > the morning to now. > Time.now includes the time, ignoring the records at morning. > > Somehow Time.now is a bit more often seen, but that may have any other > reason.I just noticed that Gearóid originally used the @today variable, so I was kind of thinking that he meant dates and not datetimes. Both would work though, assuming the field "date" is just a date. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Working perfectly, thanks a lot. On 25 Sep, 09:44, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sep 25, 9:40 am, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote: > > > > > @upcoming_events = Event.find(:all, :conditions => ["venue_id IN (?) > > AND date > ''?'' ", @nearby_venues.join(","), Time.now.to_s(:db) ], > > order => date) > > A minor tidyup: the above is equivalent to > > @upcoming_events = Event.find(:all, :conditions => ["venue_id IN (?) > AND date > ? ", @nearby_venues, Time.now ], order => ''date'') > > Active Record takes care of the boring stuff like creating the comma > separated list of ids and so on. > > Fred > > > > > so using a kind of SQL string, the variable part replaced by question > > marks > > and the values to be filled in for the ? directly following the string--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---