Christian Fazzini
2010-Oct-08 17:57 UTC
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Christian Fazzini
2010-Oct-08 18:01 UTC
How do I convert this active record query to a rails 3 query
The following SQL: select * from videos v left join artists a on a.id = v.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: Video.where({:genre => ''Acoustic'', :include => {:artist => {:user => :genre}}}) <==== Does NOT work. What am I missing? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Joshua Martin
2010-Oct-08 18:03 UTC
Re: How do I convert this active record query to a rails 3 query
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 Fazzini < christian.fazzini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- _________________________________ Joshua S. Martin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker
2010-Oct-08 18:06 UTC
Re: How do I convert this active record query to a rails 3 query
Christian Fazzini 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 workSong.where({ :genre => ''Acoustic'' }).includes({ :artist => { :user => :genre }}) -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn
2010-Oct-08 19:00 UTC
Re: Re: How do I convert this active record query to a rails 3 query
On Oct 8, 2010, at 2:06 PM, Robert Walker wrote:> Christian Fazzini 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 }})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 Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Christian Fazzini
2010-Oct-08 19:16 UTC
Re: How do I convert this active record query to a rails 3 query
Yea, thanks for the feedback. I ended up with Video.where([''genres.name = ?'', ''Acoustic'']).includes({:artist => {:user => :genre}}) which seems to work On Oct 9, 3:00 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Oct 8, 2010, at 2:06 PM, Robert Walker wrote: > > > > > Christian Fazzini 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 }}) > > 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 Biedenharn > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ > r...-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Christian Fazzini
2010-Oct-08 19:27 UTC
Re: How do I convert this active record query to a rails 3 query
Thanks Joshua, very useful link On Oct 9, 2:03 am, Joshua Martin <josmar52...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Fazzini < > > > > christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > _________________________________ > > Joshua S. Martin-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Heinz Strunk
2010-Oct-10 11:45 UTC
Re: How do I convert this active record query to a rails 3 query
Christian Fazzini wrote:> > Video.where({:genre => ''Acoustic'', :include => {:artist => {:user > => :genre}}}) <==== Does NOT work. What am I missing?What about: Video.includes(:artist => {:user => :genre}).where(:genre => ''Acoustic'') ? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.