How do I convert this active record query to a rails 3 query

The following SQL:

select * from medias m
left join artists a on a.id = m.artist_id
left join users u on u.id = a.user_id
left join genres g on g.id = u.genre_id
where g.name = ‘Acoustic’

is the equivalent to (which works):

Video.find(:all, :conditions => [‘genres.name = ?’,
‘Acoustic’], :include => {:artist => {:user => :genre}})

However, I know that the rails 3 active record query uses the .where()
clause. How can I convert the above to something like:

Song.where({:genre => ‘Acoustic’, :include => {:artist => {:user
=> :genre}}}) <==== Does NOT work

I recommend watching this Railscast (
http://railscasts.com/episodes/202-active-record-queries-in-rails-3)…
It
helped me get a handle on AR 3’s new query concept.

On Fri, Oct 8, 2010 at 1:57 PM, Christian F. <
[email protected]> wrote:

Video.find(:all, :conditions => [‘genres.name = ?’,
“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][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Joshua S. Martin

Christian F. wrote:

The following SQL:

select * from medias m
left join artists a on a.id = m.artist_id
left join users u on u.id = a.user_id
left join genres g on g.id = u.genre_id
where g.name = ‘Acoustic’

is the equivalent to (which works):

Video.find(:all, :conditions => [‘genres.name = ?’,
‘Acoustic’], :include => {:artist => {:user => :genre}})

However, I know that the rails 3 active record query uses the .where()
clause. How can I convert the above to something like:

Song.where({:genre => ‘Acoustic’, :include => {:artist => {:user
=> :genre}}}) <==== Does NOT work

Song.where({ :genre => ‘Acoustic’ }).includes({ :artist => { :user =>
:genre }})

Yea, thanks for the feedback. I ended up with
Video.where([‘genres.name = ?’, ‘Acoustic’]).includes({:artist =>
{:user => :genre}}) which seems to work

Thanks Joshua, very useful link

On Oct 8, 2010, at 2:06 PM, Robert W. wrote:

Song.where({ :genre => ‘Acoustic’ }).includes({ :artist => { :user =>
:genre }})

I think that’s going to try to “WHERE medias.genre = ‘Acoustic’”

Perhaps:
.where([‘genres.name = ?’, ‘Acoustic’])

(guessing that both Song and Video are STI subclasses of Media or else
you’ve omitted some structure that might affect the answer(s) that
you’ll receive)

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs