On 2 February 2012 08:50, Miquel Cubel <mcu...@gmail.com> 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
> 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.

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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to