Ruby on rails 注册时在设计用户对象上创建关联记录的设计注意事项

Ruby on rails 注册时在设计用户对象上创建关联记录的设计注意事项,ruby-on-rails,devise,Ruby On Rails,Devise,我正在使用Desive,对于创建的每个用户帐户,我希望生成一个关系,其中: class User < ActiveRecord::Base belongs_to :business end class Business < ActiveRecord::Base has_many :users has_one :apt_setting has_many :hours, :as => :hourable end class ApptSetting < Ac

我正在使用Desive,对于创建的每个用户帐户,我希望生成一个关系,其中:

class User < ActiveRecord::Base
  belongs_to :business
end

class Business < ActiveRecord::Base
  has_many :users
  has_one :apt_setting
  has_many :hours, :as => :hourable
end

class ApptSetting < ActiveRecord::Base
  belongs_to :business  
end
class用户:小时
结束
类ApptSetting
因此,注册后将创建一个关联的业务对象,并使用每个业务对象创建一个关联的ApptSettings和BusinessHour对象

我目前的实施方式如下:

class Admin

  before_create :create_associated_records

    def create_associated_records
      # create the associated business object
    business = Business.create(:business_name => business_name, :subdomain => subdomain, :initial_plan => initial_plan)
    # retrieve the id of the new business object
    self.business_id = business.id

    # create the associated records
    BusinessHour.default_values(business_id)
    ApptSetting.default_values(business_id)
    end
end

class ApptSetting < ActiveRecord::Base
  belongs_to :business

  def self.default_values(business_id)
    # ... create record with default values
  end

end

class BusinessHour < Hour
  belongs_to :hourable, :polymorphic => true

  def self.default_values(business_id)
    # ... create record with default values
  end

end
类管理员
创建前:创建关联的记录
def创建_关联的_记录
#创建关联的业务对象
business=business.create(:business\u name=>business\u name,:subdomain=>subdomain,:initial\u plan=>initial\u plan)
#检索新业务对象的id
self.business\u id=business.id
#创建关联的记录
BusinessHour.default_值(business_id)
ApptSetting.默认值(业务id)
结束
结束
类ApptSettingtrue
def自我默认值(业务id)
# ... 使用默认值创建记录
结束
结束
这确实有效,但它看起来是最好的设计吗

我正在考虑的另一种选择是处理删除Admin->create_关联的_记录,而不是在Users::Accounts::RegistrationsController中执行该操作,在这里我重写了“create”方法。在那里,我可以构建所有关联的记录,set:accepts\u嵌套的\u属性,然后在业务对象上调用“save”,这将导致生成所有关联的记录


关于最佳设计的想法或任何其他想法?

您不需要使用默认值方法。在“创建\u关联的\u记录”中,您可以将这些调用更改为:

ApptSetting.create(:business_id => business_id)
不要重写create方法。在创建之前,回调是一种更好的方法。在这两种情况下,如果一个企业有许多用户,您真的想在每次创建新用户时创建一个新企业吗?如何将第二个用户添加到业务中?加上

def create_associated_records
  return unless self.business_id.nil?
  ....
此外,方法中的业务名称、子域和初始计划变量来自何处?您是否将它们作为管理员用户的属性?似乎它们应该是企业的唯一价值

我认为这里最大的问题是,用户真的需要一个企业才能生存吗?为什么用户不能在创建帐户后创建业务

**编辑:使用rails关联方法获得更清晰/更清晰的版本:

class Admin

  before_create :create_associated_records

  private

  def create_associated_records
    return unless self.business_id.nil?
    self.create_business
    self.business.create_appt_setting
    self.business.hours.create
  end

end

此外,在这种情况下,什么是应用程序设置?是预约设置吗?喜欢时间吗?一个企业真的只有一次约会吗?1)因此,如果我删除默认值,我可以在initialize中只使用默认值,而不必直接调用它,对吗?2) 我计划在此之后处理一个用户注册以添加到现有业务帐户的情况。但现在我考虑的是创建的初始业务帐户,它必须至少有一个用户。在这种情况下,它们应该同时创建,所以是的,业务必须在用户之前存在。3) apptsetting不是times,而是定制消息。哦,我明白了,我将调用before\u create of before\u save来设置默认值添加一个更清晰/更清晰的版本,该版本还使用rails关联方法。在这种情况下,您不需要担心填充ID,因为rails将为您完成这项工作。此外,如果每次创建业务时都需要创建appt_设置和一个小时,则应该在创建方法之前将这些调用移动到业务中。是的,您可以使用before\u create在每个模型中设置默认值,或者您可能希望使用before\u验证