ruby on rails architecture model, view and DTOs -
i .net guy , try understand concept behind rails , active record stuff.
as can see in examples assume view 1:1 copy of model. in reality that´s not true.
like view holds customer and contact person(s) not related customer. user should able edit both (customer , contact person(s) in 1 view e.g.)
in every example see bind view directly 1 activerecord object. stuff model, validation , on bind 1 object mapped directly database.
could rails guy explain what´s elegant way work active record in real life applications in complex model situations? in first moment thinking dtos not imagine way go rails.
agree john....
you asked: "like view holds customer , contact person(s) not related customer. user should able edit both (customer , contact person(s) in 1 view e.g.)"
ok, customer model has employees, right? if not, replace "employee" "person"
/app/model/customer.rb
class customer < activerecord::base has_many :employees accepts_nested_attributes_for :employees end
/app/model/employee.rb
class employee < activerecord::base belongs_to :customer end
then in view of customer /app/views/customers/show.html.erb
<%= form_for(@customer) |f| %> <%= f.text_field :name %> .... yada yada .... <%= f.fields_for(:employees) |ef} %> <%= ef.text_field :first_name%> <%= ef.text_field :phone %> <% end %> <%= f.submit %> <% end %>
the above has 1 form lets save customer , it's employees. called nested form, , think job of needing "view models".
if keep grouped how real-life grouped, goes pretty simple.
Comments
Post a Comment