sakthivel sekar
2012-Dec-01 14:08 UTC
Active Record Query to select associated model values.
Hi, I have two models named Customer and Order. Customer < ActiveRecord::Base has_many :orders end Order < ActiveRecord::Base belongs_to :customer end Customer model has attributes like first_name and last_name. Order has customer_id along with other attributes. Now, I want Customer first_name for specific order. I can get it with following query. Customer.joins(:orders).where(''orders.id = 1'').select(''first_name''). But, how to get it along with attributes of order table also. Say, I want both customers first_name and orders table created_at value. When I try, Order.includes(:customer).select(''orders.created_at, customers.first_name'') this returns only order table records. What is the best way to select attributes of joined tables also with Active Record Query. Thanks. -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ueZg0GTIwiUJ. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2012-Dec-01 15:43 UTC
Re: Active Record Query to select associated model values.
On 1 December 2012 14:08, sakthivel sekar <sakthivelsekar33-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > I have two models named Customer and Order. > > Customer < ActiveRecord::Base > has_many :orders > end > > Order < ActiveRecord::Base > belongs_to :customer > end > > Customer model has attributes like first_name and last_name. Order has > customer_id along with other attributes. > > Now, I want Customer first_name for specific order. I can get it with > following query. > > Customer.joins(:orders).where(''orders.id = 1'').select(''first_name'').You are missing the fundamental nature of ActiveRecord associations. If you have an order in, for example, @order then the customer is @order.customer and if you want that customer''s first_name then you can say @order.customer.first_name. There is no need to mess about with complex queries, such is the magic of Rails.> > But, how to get it along with attributes of order table also. Say, I want > both customers first_name and orders table created_at value. > When I try, > Order.includes(:customer).select(''orders.created_at, customers.first_name'') > this returns only order table records. What is the best way to select > attributes of joined tables also with Active Record Query.Have a look at the Rails Guide on ActiveRecord Associations (and the other guides for that matter) and work right through a good tutorial such as railstutorial.org (which is free to use online) and you will be able to answer most of these questions yourself. 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 https://groups.google.com/groups/opt_out.
sakthivel sekar
2012-Dec-11 17:08 UTC
Re: Active Record Query to select associated model values.
Colin, Thanks for the reply. Yes, association makes this better and easy way. But I''m expecting a way apart from using association. I heard *merge* will do this, but I''m not sure. On Saturday, December 1, 2012 7:38:50 PM UTC+5:30, sakthivel sekar wrote:> > Hi, > I have two models named Customer and Order. > > Customer < ActiveRecord::Base > has_many :orders > end > > Order < ActiveRecord::Base > belongs_to :customer > end > > Customer model has attributes like first_name and last_name. Order has > customer_id along with other attributes. > > Now, I want Customer first_name for specific order. I can get it with > following query. > > Customer.joins(:orders).where(''orders.id = 1'').select(''first_name''). > > But, how to get it along with attributes of order table also. Say, I want > both customers first_name and orders table created_at value. > When I try, > Order.includes(:customer).select(''orders.created_at, customers.first_name'') > this returns only order table records. What is the best way to select > attributes of joined tables also with Active Record Query. > > Thanks. > >-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/G1nZqYErdJgJ. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2012-Dec-11 17:13 UTC
Re: Re: Active Record Query to select associated model values.
On 11 December 2012 17:08, sakthivel sekar <sakthivelsekar33-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Colin, > Thanks for the reply. Yes, association makes this better and easy way. But > I''m expecting a way apart from using association. I heard merge will do > this, but I''m not sure.Why do you want to do it a way other than (as you say) the easiest? Colin> > > On Saturday, December 1, 2012 7:38:50 PM UTC+5:30, sakthivel sekar wrote: >> >> Hi, >> I have two models named Customer and Order. >> >> Customer < ActiveRecord::Base >> has_many :orders >> end >> >> Order < ActiveRecord::Base >> belongs_to :customer >> end >> >> Customer model has attributes like first_name and last_name. Order has >> customer_id along with other attributes. >> >> Now, I want Customer first_name for specific order. I can get it with >> following query. >> >> Customer.joins(:orders).where(''orders.id = 1'').select(''first_name''). >> >> But, how to get it along with attributes of order table also. Say, I want >> both customers first_name and orders table created_at value. >> When I try, >> Order.includes(:customer).select(''orders.created_at, >> customers.first_name'') >> this returns only order table records. What is the best way to select >> attributes of joined tables also with Active Record Query. >> >> Thanks. >> > -- > 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 > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/G1nZqYErdJgJ. > > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.