Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are view models used in rails?

Tags:

I'm starting to develop a small application in ruby on rails and many questions arise. I should say that I have about 1 year of experience with ASP.NET MVC and feel at home with models views and controllers. I've been using view models extensively (with the help of AutoMapper) and now wondering if view models are used similarly in rails camp.

From various examples (rails casts mainly) I've gathered that it is common to either combine data from multiple models right in your view (which is frowned upon in ASP.NET MVC), or to use virtual attributes on models to obtain "missing" data.

I know that business model should not be modelled after UI needs, for example there should not be a 'password confirm' property in your model, this should be a view model property.

Rails virtual properties seem to violate this principle.

How is it done in rails?

Thanks.

like image 566
Valentin V Avatar asked Mar 26 '10 06:03

Valentin V


People also ask

What are models in Rails?

A Rails Model is a Ruby class that can add database records (think of whole rows in an Excel table), find particular data you're looking for, update that data, or remove data. These common operations are referred to by the acronym CRUD--Create, Remove, Update, Destroy.

What is model-view-controller in Rails?

Rails has an application directory called app/ with three subdirectories: models, views, and controllers. This is the model-view-controller (MVC) architectural pattern, which enforces a separation between business logic from the input and presentation logic associated with a graphical user interface (GUI).

How does Ruby on Rails use the model-view-controller framework?

Like most of the other frameworks, Rails is also based on MVC pattern. It basically works as following: Requests first come to the controller, controller finds an appropriate view and interacts with model which in turn interacts with database and send response to controller.

Does Ruby use MVC?

Ruby on Rails does not implement the MVC design pattern. Ruby on Rails has folders called controllers, models, and views.


1 Answers

If I understand the notion of View Model correctly, it's a concept that's not immediately obvious in Rails, but neither is it forbidden/frowned-on or otherwise not allowed. There's no specific requirement for a perfect one-to-one mapping of models to tables so you're free to work at the level of abstraction that's appropriate.

I don't think referencing multiple models in a view is considered particularly bad (if it is, then I have some areas that need a little rework) although updating multiple models from a single view can get a bit tricky.

Anyway, in Rails I think we're talking about what seems to be mostly referred to as the "Presenter" pattern. Some references (Google "Rails Presenter Pattern" for more than you probably want or need):

  • http://blog.jayfields.com/2007/03/rails-presenter-pattern.html
  • http://gilesbowkett.blogspot.com/2007/10/my-version-of-rails-presenters.html
  • http://blog.jayfields.com/2007/10/extending-rails.html (mentions that DHH used presenters in Highrise)
like image 69
Mike Woodhouse Avatar answered Oct 16 '22 14:10

Mike Woodhouse