On Wednesday, May 25, 2011 11:22:24 AM UTC-6, CarlosCD wrote:
>
> Kind of makes sense that it failed based on the SQL sentence it produces.
> What about something like this:
>
Yeah, it failed because I introduced two typos. My bad (that's what I get
for not double-checking before posting).
Try the corrected:
Event.joins(:venue).where(:venues => { :locality => "London" })
Notice joins has been fixed (as you figured out on your own) but also that
the symbol :venue was pluralized to :venues. This should work (I even did a
quick test app and verified it does for me).
>
> Event.find_by_sql("select events.* FROM events INNER JOIN venues ON
> venues.id = events.venue_id WHERE venue.locality = 'London'")
>
> Let me know,
> C
>
> On Wed, May 25, 2011 at 5:49 AM, Michael Baldock <[email protected]>wrote:
>
>> Ah ok,
>>
>> I had read this document, the reason I didn't find what I was looking
>> for was that the associations in here were all opposite to my needs, eg
>> - one of the examples is
>>
>> time_range = (Time.now.midnight - 1.day)..Time.now.midnight
>> Client.joins(:orders).where(:orders => {:created_at => time_range})
>>
>> where a Client presumably has_many orders, and an Order belongs_to a
>> client.
>>
>> I was looking for something like as you say:
>>
>> Event.join(:venue).where(:venue => { :locality => "London" })
>>
>> where an Event belongs_to a Venue, and a venue has many events. looking
>> that way round
>>
>> Unfortunately, the exact line you give returns
>>
>> undefined method `join' for #<Class:0x104b2fd68>
>>
>> trying 'joins' instead returns:
>>
>> SQLite3::SQLException: no such column: venue.locality: SELECT "events".*
>> FROM "events" INNER JOIN "venues" ON "venues"."id" = "events"."venue_id"
>> WHERE ("venue"."locality" = 'London')
>>
>> there definitely is a column in venue called locality!
>>
>> I think I tried this as part of my searching, but kept getting errors
>> along these lines.
>>
>> Is there an easy way to make the query, when an Event belongs_to a
>> Venue, and I'm searching using an attribute in the Venue model.
>>
>> Thanks for your help
>>
>> Mike
>>
>> --
>> 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 [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/rubyonrails-talk?hl=en.
>>
>>
>
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.