Tuesday, May 24, 2011

[Rails] Re: return models using association sort

On Tuesday, May 24, 2011 12:33:40 PM UTC-6, Ruby-Forum.com User wrote:
Hi all,

Think this is a really simple question, but so far cant find answers
after much searching!

I have 2 models, Event and Venue, an Event belongs_to a Venue.
A venue has an attribute locality, which is a string
I want to get an array of events, for which the venue they belong_to has
the locality attribute equal to London. A list of events in London!

That's all! I can think of a horrible way to do this, where I return all
events, go through the array, adding to a new array events where
event.venue.locality == "london", but this seems daft, is there a way I
can do this in a query, something like

@ed_events = Event.where(Venue.locality => "London")

I've found lots of help on more complicated joins between tables, but
cant find the syntax for this!

Was this one of the resources your read (from the Rails Guides):

http://guides.rubyonrails.org/active_record_querying.html#joining-tables
 
Specifically read section 3.2, "Using Array/Hash of Named Association"

You should be able to do something like:

Event.join(:venue).where(:venue => { :locality => "London" })

Hope this helps.

--
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@googlegroups.com.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate