maskiran
2011-Sep-18 00:49 UTC
tables association and accessing them in views - newbie question
Hi I am new to rails and have a very simple scenario like mentioned here table: photos id:int user_id:int table: users id:int email:string In the photos model i said it belongs_to :user When I access the photos.all, I can get the user email by photos.first.user.email If I want to display 100 photos on a page, the email is accessed as above it goes to the database at that time to get the value 1) Am I violating MVC here as the database query is being sent (though not by me directly) in the view? 2) Since the query goes 100 times in the view, should there be a better way to get all the data at once in the controller/model and pass all that to the view. I am coming from PHP world where I used to write queries and there I used to do a join on photos table and got all the data at once. Is this join costlier than getting data in each query as above Thanks Kiran -- 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.
Walter Lee Davis
2011-Sep-18 03:34 UTC
Re: tables association and accessing them in views - newbie question
Try :include => :user when you request the photos, and that will grab the user in the same request as the photo. Walter On Sep 17, 2011, at 8:49 PM, maskiran wrote:> Hi > I am new to rails and have a very simple scenario like mentioned here > > table: photos > id:int > user_id:int > > table: users > id:int > email:string > > In the photos model i said it belongs_to :user > > When I access the photos.all, I can get the user email by > photos.first.user.email > > If I want to display 100 photos on a page, the email is accessed as > above it goes to the database at that time to get the value > > 1) Am I violating MVC here as the database query is being sent (though > not by me directly) in the view? > 2) Since the query goes 100 times in the view, should there be a > better way to get all the data at once in the controller/model and > pass all that to the view. > > I am coming from PHP world where I used to write queries and there I > used to do a join on photos table and got all the data at once. Is > this join costlier than getting data in each query as above > > Thanks > Kiran > > -- > 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. >-- 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.
Michael Pavling
2011-Sep-18 06:38 UTC
Re: tables association and accessing them in views - newbie question
On 18 September 2011 01:49, maskiran <maskiran-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When I access the photos.all, I can get the user email by > photos.first.user.email > > 1) Am I violating MVC here ?No you''re not, as it''s the model (via ActiveRecord) doing the query - that''s exactly how you''re supposed to do it :-) (but do try to avoid directly accessing models in views; don''t do "Photos.first.user.email" in an erb file... have @photos populated from the controller so you can call @photos.first.user.email in the view)> 2) Since the query goes 100 times in the view, should there be a > better way to get all the data at once in the controller/model and > pass all that to the view. > > I am coming from PHP world where I used to write queries and there I > used to do a join on photos table and got all the data at once. Is > this join costlier than getting data in each query as aboveAs Walter says, :include the :user in the photos query to eagerly load them. This way Rails will do the DB join and return all the data for you. -- 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.