0

I have a small question about collection_select in Rails 4. When we call in the collection_select all the elements of a table example:

<%= f.collection_select :category_id, Category.all, :id, :name %>

Is MVC violated? In other words: if I have a collection_select in the view of products and it calls Category.all in this view, for me we are violated the MVC pattern. Cause Category.all is something that should be in a Model.

Tell me if I'm in right.

rene
  • 41,474
  • 78
  • 114
  • 152
Vito
  • 746
  • 2
  • 9
  • 25
  • 1
    Depends on your definition of MVC, and how much you care about view isolation. Really, having the action instance variables is *more* of a violation, IMO-a view, ideally, would only have access to a view-specific model. There are almost always trade offs. – Dave Newton May 14 '15 at 19:51
  • Umm , so in which case i can talk about violation? – Vito May 14 '15 at 19:58
  • If it's important to you, sure-but I'm not really sure it's a big deal. "Pure" MVC frameworks are few and far between, although with the proliferation of Web sockets and similar they're becoming more doable. – Dave Newton May 14 '15 at 20:43

1 Answers1

0

Passing a collection to collection_select as a list of options is not a violation of MVC pattern, it's just how collection_select is seeded with data.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • Maybe i should use a method to take all the categories..or not? – Vito May 14 '15 at 19:34
  • @Vito the thing about method is that it would be evaluated only when class is loaded, which means it could be not accurate enough. – Andrey Deineko May 14 '15 at 19:35
  • Sorry Andrey, but i still really don t understand why , when i call Category.all in a view , it's not a violation. These kind of things should be done in the Model of Category and recalled in the view through an helper or a simpli method. Thx for helping me :) – Vito May 14 '15 at 19:40
  • @Vito Not in `Category`, but rather a model that goes to the view. – Dave Newton May 14 '15 at 20:27