I am developing one simple demo application for room reservation where I have three tables as member_types, customers, and donors
我正在为房间预订开发一个简单的演示应用程序,其中我有三个表作为member_types,客户和捐赠者
The models are as follows
模型如下
class MemberType < ActiveRecord::Base
has_one :donor
has_many :customers
has_many :room_rates
attr_accessible :member_type
end
class Customer < ActiveRecord::Base
belongs_to :member_type
has_one :donor
has_many :rooms
has_many :room_types
has_many :room_rates
end
class Donor < ActiveRecord::Base
belongs_to :customer
belongs_to :member_type
attr_accessible :donation, :free_days, :used_days,:customer_id
end
I have added member_types as donor, guest and two other as admin when customers add their basic info with select member_type as donor and submitting it stores all info in the customer table
当客户使用select member_type作为捐赠者添加他们的基本信息并且提交它将所有信息存储在客户表中时,我添加了member_types作为捐赠者,访客和另外两个作为管理员
How can I add this customer_id in donor table when customer submit their info?
当客户提交信息时,如何在捐赠者表格中添加此customer_id?
1 个解决方案
#1
0
You can use active record callback in Customer model, something like this:
您可以在Customer模型中使用活动记录回调,如下所示:
class Customer< ActiveRecord::Base
after_save :new_donor
private
def new_donor
new_record = Donor.new(:customer_id => self.id)
new_record.save
end
end
Btw, you have a closed loop relation in your model design (room_rates is belongs to member_type and customer) which is not a best practice in table design although it's not totally wrong either.
If possible, you may want to redesign it
顺便说一下,你的模型设计中有一个闭环关系(room_rates属于member_type和customer),这不是表设计中的最佳实践,尽管它也不是完全错误的。如果可能,您可能需要重新设计它
#1
0
You can use active record callback in Customer model, something like this:
您可以在Customer模型中使用活动记录回调,如下所示:
class Customer< ActiveRecord::Base
after_save :new_donor
private
def new_donor
new_record = Donor.new(:customer_id => self.id)
new_record.save
end
end
Btw, you have a closed loop relation in your model design (room_rates is belongs to member_type and customer) which is not a best practice in table design although it's not totally wrong either.
If possible, you may want to redesign it
顺便说一下,你的模型设计中有一个闭环关系(room_rates属于member_type和customer),这不是表设计中的最佳实践,尽管它也不是完全错误的。如果可能,您可能需要重新设计它