Mahmoud Abdel-Fattah
2011-Nov-11 12:02 UTC
newbie question - How can I run the following Query in ActiveRecord ?
I''m totally rails newbie, and just started it a couple of days ago moving from PHP, so I''ve the following query, and want to know how can I do it using ActiveRecord SELECT `sites`.*, `snapshots`.*, `technologies`.* FROM `sites`, `snapshots`, `technologies` WHERE `sites`.id = ''1'' AND `snapshots`.`site_id` = `sites`.`id` AND `technologies`.`snapshot_id` = `snapshots`.`id` -- 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.
Colin Law
2011-Nov-11 13:16 UTC
Re: newbie question - How can I run the following Query in ActiveRecord ?
On 11 November 2011 12:02, Mahmoud Abdel-Fattah <mahmoud-n4k1QsQJifu9aiDbRAtm+A@public.gmane.org> wrote:> I''m totally rails newbie, and just started it a couple of days ago > moving from PHP, so I''ve the following query, and want to know how can > I do it using ActiveRecord > > SELECT `sites`.*, `snapshots`.*, `technologies`.* > FROM `sites`, `snapshots`, `technologies` > WHERE `sites`.id = ''1'' > AND `snapshots`.`site_id` = `sites`.`id` > AND `technologies`.`snapshot_id` = `snapshots`.`id`Don''t think about queries, think about relationships. If you setup the has_many and belongs_to relationships then if you have a Site in @site then its snapshots are @sites.snapshots and for a given snapshot then its technologies are snapshot.technologies. Let Rails worry about the queries. Any newcomer is well advised to work through some tutorials to understand what rails can do. railstutorial.org is good and is free to use online. Also work through the Rails Guides. Make sure any tutorial you use is for the right version of Rails. Colin> > -- > 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. > >-- gplus.to/clanlaw -- 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.
Mahmoud Said
2011-Nov-11 15:49 UTC
Re: newbie question - How can I run the following Query in ActiveRecord ?
I think what you are trying to do is joins
if your associations are set correctly, then something like below should
work
Site.joins(:snapshots).joins(:technologies).where("sites.id=1")
*Note: *in the where clause, you cannot use :id=>1 because :id with the
joined tables will be ambigious... you have to state explicitly the table
of which the id to be used
On Fri, Nov 11, 2011 at 2:02 PM, Mahmoud Abdel-Fattah <
mahmoud-n4k1QsQJifu9aiDbRAtm+A@public.gmane.org> wrote:
> I''m totally rails newbie, and just started it a couple of days ago
> moving from PHP, so I''ve the following query, and want to know how
can
> I do it using ActiveRecord
>
> SELECT `sites`.*, `snapshots`.*, `technologies`.*
> FROM `sites`, `snapshots`, `technologies`
> WHERE `sites`.id = ''1''
> AND `snapshots`.`site_id` = `sites`.`id`
> AND `technologies`.`snapshot_id` = `snapshots`.`id`
>
> --
> 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.
>
>
--
Mahmoud Said
Software Engineer - eSpace
blog.modsaid.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.