1

Hello I have a model named Client which has nested models called Receiver and Receipt.Basically a client donate money to a receiver or many receivers, and that gift appear in a receipt.

model/client.rb

class Client < ActiveRecord::Base
  has_many :receivers
  has_many :receipts

  accepts_nested_attributes_for :receivers
  accepts_nested_attributes_for :receipts
end

views/client/_form.html.erb

<%= simple_form_for @client do |f| %>
  <%= f.input :name %>
  <%= f.input :input %>
  <%= f.input :suscribtion_number %>
  <%= simple_fields_for :orders do |o| %>
    <%= o.input :name %>
  <% end %>
  <%= f.button :submit %>
<% end %>

Matter fact how can I dynamically transfer a client "input" to 1- an oder's "amount"(attribute) 2- and to a receipt's "amount"(attribute)

2
  • You shouldn't shadow your form builder variable f. Give the second one another name, for example.
    – tbuehlmann
    Commented Nov 29, 2011 at 22:42
  • 1
    Sorry that's some bad omission by myself.I've fixed it in the question.
    – blawzoo
    Commented Nov 29, 2011 at 23:07

1 Answer 1

1

You can simply do @receipt.amount = @client.amount. However, the better way to model this would be to have a Donation model with an amount attribute. Then, link clients and receivers to the donation.

The donation model would probably replace your receipt model. When you need to compute how much a client has donated, simply sum all the associated donation amounts.

By modeling the donation, you won't need to worry about keeping multiple copies of the same information (i.e. the donation amount) in sync. Having multiple copies of the same information is a bad idea, generally speaking.

1
  • Thanks David Sulc.I'll give the Donation model a shot.I don't even think about that aspect of things.
    – blawzoo
    Commented Nov 29, 2011 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.