<html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> Hi,<br> <br> I have two doubts about the right way / place to write some code.<br> <br> Question 1:<br> I have a product and in the view I need to show a listbox with all the categories of this product.<br> - Option1: in the controller make "<font color="#3366ff">@product_categories = ProductCategori.all</font>" and later in the view make "<font color="#3366ff"><%= f.collection_select(:product_category_id, @product_categories, :id, :name) %></font>"<br> - Option2: in the view just write "<font color="#3366ff"><% f.collection_select(:product_category_id, ProductCategori.all, :id, :name) %></font>"<br> <br> Question 2: <br> I need to list products with some complex logic<br> - Option1: in the controller make "<font color="#3366ff">@products = Products.list(param1, param2)</font>" and in the model "<font color="#3366ff">def self.list(param1, param2)</font>" with all the options joins, where, ...<br> - Option2: Put all the logic in the controller and avoid calling the model<br> <br> Greetings<br> <pre class="moz-signature" cols="72">-- Miquel Cubel Escarré <a class="moz-txt-link-freetext" href="http://railsdynamics.blogspot.com">http://railsdynamics.blogspot.com</a> +34 699 73 22 46 <a class="moz-txt-link-abbreviated" href="mailto:mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org">mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org</a> </pre> </body> </html> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.<br /> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org<br /> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org<br /> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.<br />
On 2 February 2012 08:50, Miquel Cubel <mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have two doubts about the right way / place to write some code. > > Question 1: > I have a product and in the view I need to show a listbox with all the > categories of this product. > - Option1: in the controller make "@product_categories > ProductCategori.all" and later in the view make "<%> f.collection_select(:product_category_id, @product_categories, :id, :name) > %>" > - Option2: in the view just write "<%> f.collection_select(:product_category_id, ProductCategori.all, :id, :name) > %>"I would use option 1. The principle reason is that then if under some circumstances you don''t want to show them all then the logic can go in the controller, leaving the view alone.> > Question 2: > I need to list products with some complex logic > - Option1: in the controller make "@products = Products.list(param1, > param2)" and in the model "def self.list(param1, param2)" with all the > options joins, where, ... > - Option2: Put all the logic in the controller and avoid calling the > modelNever put logic in the controller if it can reasonably go in the model. Then if the logic changes (maybe you change a detail of how the data is stored in the database) this affects only the model and not the controller also. Rather than a simple method a scope may be more appropriate if the purpose is to select a set of records from the db. That is what scopes are for. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for the answer El 02/02/2012 10:09, Colin Law escribió:> On 2 February 2012 08:50, Miquel Cubel<mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi, >> >> I have two doubts about the right way / place to write some code. >> >> Question 1: >> I have a product and in the view I need to show a listbox with all the >> categories of this product. >> - Option1: in the controller make "@product_categories >> ProductCategori.all" and later in the view make "<%>> f.collection_select(:product_category_id, @product_categories, :id, :name) >> %>" >> - Option2: in the view just write "<%>> f.collection_select(:product_category_id, ProductCategori.all, :id, :name) >> %>" > I would use option 1. The principle reason is that then if under some > circumstances you don''t want to show them all then the logic can go in > the controller, leaving the view alone.I agree with this point of view, but in the other side, I find myself repeating "@product_categories = ProductCategori.all" in several controllers, like "new" and "edit", and later I found that with default scaffold, after submitting, if there is an error, this assignation doesn''t work well, and I don''t understand exactly why (in rails 3.1.3).> >> Question 2: >> I need to list products with some complex logic >> - Option1: in the controller make "@products = Products.list(param1, >> param2)" and in the model "def self.list(param1, param2)" with all the >> options joins, where, ... >> - Option2: Put all the logic in the controller and avoid calling the >> model > Never put logic in the controller if it can reasonably go in the > model. Then if the logic changes (maybe you change a detail of how > the data is stored in the database) this affects only the model and > not the controller also. > Rather than a simple method a scope may be more appropriate if the > purpose is to select a set of records from the db. That is what > scopes are for.Completely agree!> > Colin >Thanks for clearing it up. -- Miquel Cubel Escarré http://railsdynamics.blogspot.com +34 699 73 22 46 mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org -- 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.
On 2 February 2012 15:14, Miquel Cubel <mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for the answer > > El 02/02/2012 10:09, Colin Law escribió: > >> On 2 February 2012 08:50, Miquel Cubel<mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> >>> Hi, >>> >>> I have two doubts about the right way / place to write some code. >>> >>> Question 1: >>> I have a product and in the view I need to show a listbox with all >>> the >>> categories of this product. >>> - Option1: in the controller make "@product_categories >>> ProductCategori.all" and later in the view make "<%>>> f.collection_select(:product_category_id, @product_categories, :id, >>> :name) >>> %>" >>> - Option2: in the view just write "<%>>> f.collection_select(:product_category_id, ProductCategori.all, :id, >>> :name) >>> %>" >> >> I would use option 1. The principle reason is that then if under some >> circumstances you don''t want to show them all then the logic can go in >> the controller, leaving the view alone. > > I agree with this point of view, but in the other side, I find myself > repeating "@product_categories = ProductCategori.all" in several > controllers, like "new" and "edit",Put it in a before_filter specifying the actions that need it.> and later I found that with default > scaffold, after submitting, if there is an error, this assignation doesn''t > work well, and I don''t understand exactly why (in rails 3.1.3).I think you had better ask about that in a different thread if you can''t sort it out. Have you read the Rails Guide on Debugging which shows techniques for debugging your code. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.