[Rails] Relationship navigation issue

Adam Denenberg
Tue, 31 Jan 2006 22:01:29 -0000

you need to use a join.  I think i did see someone use the :through
paramater on here but i dont see documentation of it anywhere.

select * from events left join venues on events.venue_id = venue.id
left join cities on venue.city_id = city.id left join state on
city.state_id = state.id where state.name = ''NY''

adam


On 1/31/06, Neeraj Kumar <neeraj.jsr@gmail.com>
wrote:
> class Event < ActiveRecord::Base
>    belongs_to  :venue
>    belongs_to  :category
>  end
>
>  class Venue < ActiveRecord::Base
>    belongs_to  :city
>  end
>
>  class City < ActiveRecord::Base
>    belongs_to  :state
>  end
>
>  I want to retrieve all event records belonging to a state. Coming from
> hibernate background I tried something like this:
>
>   def self.list_for_a_state(state_id)
>      find_by_sql(["select e.* from venues v, events e where
v.city.state_id> ? and e.venue_id = v.id",state_id])
>   end
>
>  Error: Unknown table ''v.city'' in where clause
>
>  How do I handle this case?
>
>  Thanks.
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>