On Wednesday, May 25, 2011 11:36:58 AM UTC-6, CarlosCD wrote:
>
> If you look the SQL sentence it produces, kind of makes sense that it
> failed... What about something like this:
>
It only only makes sense in that the original version I posted has a typo
(missing 's' in two places). When fixed, it works fine.
>
> 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
>
>
>
SQL created the the code in my last post:
SELECT "events".* FROM "events" INNER JOIN "venues" ON "venues"."id" =
"events"."venue_id" WHERE "venues"."locality" = 'London'
This works. Just checked it again. There is no need for Event.find_by_sql
(which is an ugly last resort).
This is what i did to verify correctness:
$ rails new example
...
$ cd example
$ bundle install
...
$ rails g scaffold venue name:string locality:string
...
$ rails g scaffold event venue:references name:string
...
$ rake db:migrate
...
$ vi app/models/venue.rb
$ cat app/models/venue.rb
class Venue < ActiveRecord::Base
has_many :events
end
$ cat app/models/event.rb
class Event < ActiveRecord::Base
belongs_to :venue
end
$ rails c
Loading development environment (Rails 3.0.7)
> Venue.create :name => "The Globe Theatre", :locality => "London"
=> <#<Venue id: 1, name: "The Globe Threatre", locality => "London", ...>
> Event.create :name => "Hamlet", :venue => Venue.find(1)
=> #<Event id: 1, venue_id: 1, name: "Hamlet", ...>
> puts Event.joins(:venue).where(:venues => {:locality => "London"}).to_sql
SELECT "events".* FROM "events" INNER JOIN "venues" ON "venues"."id" =
"events"."venue_id" WHERE "venues"."locality" = 'London'
=> nil
> x = Event.joins(:venue).where(:venues => {:locality => "London"})
=> [#<Event id: 1, venue_id: 1, name: "Hamlet", ...>]
--
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.