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.
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.
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).
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.
Ruby on Rails does not implement the MVC design pattern. Ruby on Rails has folders called controllers, models, and views.
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):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With