Are view models used in rails?
Asked Answered
G

1

26

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.

Ghiselin answered 26/3, 2010 at 6:44 Comment(1)
ASP.NET MVC does have the concept of pseudo-multiple-models in that they typically just wrap many of them in a single object that you write yourself.Overweening
W
18

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):

Wolcott answered 26/3, 2010 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.