同一按钮上的两个不同动作

时间:2013-04-05 09:42:08

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我正在为房间预订开发一个简单的演示应用程序 我有三个表作为member_types,客户和捐赠者

模型如下

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

我已将member_types添加为捐赠者,访客和另外两名作为管理员 当客户使用select member_type作为捐赠者添加他们的基本信息并提交时,它将所有信息存储在客户表

当客户提交信息时,如何在捐赠者表格中添加此customer_id?

1 个答案:

答案 0 :(得分:0)

您可以在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

顺便说一下,你的模型设计中有一个闭环关系(room_rates属于member_type和customer),这不是表设计中的最佳实践,尽管它也不是完全错误的。
如果可能,您可能需要重新设计

相关问题