在设计用户对象创建相关的记录我使用的设计,并为创建的每个用户帐户,我想生成一个关系,其中:设计考虑上注册
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
所以在登记相关的业务对象创建,并且每个业务对象都会创建一个关联的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
这并不工作,但它似乎是最好的设计?
一种选择,我考虑的是处理消除管理 - > create_associated_records,而是做用户的工作::账户:: RegistrationsController,我重写“create”方法。在那里我可以建立所有关联的记录,在适当的地方设置:accepting_nested_attributes,然后在Business对象上调用'save',这会导致生成所有关联的记录。
思考的最佳设计,或任何其他的想法?
还,什么是这方面的一个ApptSetting?是预约设置?像时间?一个企业真的只有一次的约会? – MissingHandle
1),所以如果我删除default_values我可以只默认值初始化,而不必直接调用它,对吗? 2)我计划处理用户在此之后注册添加到现有企业帐户的情况。但就目前而言,我正在考虑创建最初的企业帐户,它必须至少有一个用户。在这种情况下,他们应该同时创建,所以是的,一个企业必须存在于用户之前。 3)ApptSettings不是时候,而是像定制的消息。 – 99miles
哦,我明白了,我会打电话给before_create before_save的设置默认值 – 99miles