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)
You can simply do
@receipt.amount = @client.amount. However, the better way to model this would be to have aDonationmodel with anamountattribute. Then, link clients and receivers to the donation.The
donationmodel would probably replace yourreceiptmodel. 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.