Average Price - Appropriate logic to execute in view, or better in controller?

Would it be nice to calculate the "average price" as shown below in the view?

Or is it against MVC and is it better to do it in a controller?

<p>Average price: <%= @seller.total_sales / @seller.num_sales %></p>

      

0


a source to share


3 answers


Neither. Place it in the model. Then unit test becomes easier.



+4


a source


Ask a few questions:

Will this average price be displayed Is this part of the view (is it used to display something?) Does it need complex things to get / calculate / fetch or whatever?



If you think this is just a prompt for your user, it is only used once and you can leave it in your view.

But if you feel uncomfortable with this, or need to do more complex math on price, put it in your model.

0


a source


Place your business logic where it belongs to the model:

<p>Average price: <%= @seller.get_average_price () %></p>

      

0


a source







All Articles