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.
On Sep 24, 4:53 pm, Gearóid O’Ceallaigh [email protected] wrote:
Thanks in advance.
You need to correct your :conditions
:conditions => {:location_id => @location.id}
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?
@nearby_venues = Venue.find(:all, :conditions => {:location_id =
@location.id}, :order => ‘name’)
should work in that case
On Sep 25, 9:20 am, Gearóid O’Ceallaigh [email protected] 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
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
On Sep 25, 9:40 am, Thorsten Müller [email protected] 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
On Sep 25, 4:44 pm, Frederick C. [email protected]
wrote:
On Sep 25, 9:40 am, Thorsten Müller [email protected] 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.
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.
Working perfectly, thanks a lot.
On Sep 25, 6:01 pm, Thorsten Müller [email protected] wrote:
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.