创建新帐户时,我的代码类似于以下内容作为after_create回调:
def after_account_create
new_account = self
logger.info( '%%%%%%%%%%%%%%%%%%%%% account is = ' + new_account.inspect)
assessment = Assessment.new :title => "Cake baking", :description => "Every day we bake fresh cakes to sell in our shop. This is our assessment of the risks involved in this process.", :account_id => new_account.id
assessment.save!
end
当我在我的开发环境(Passenger / Postgres)上运行它时,它运行正常 - 每当一个人注册并创建一个新帐户时,它就会创建一个分配给该个人帐户的新评估。
但是,当我在我的生产环境(Heroku上的瘦网络服务器)上运行此操作时,每次个人注册并创建新帐户时,它都会创建一个新的评估,但会分配给上个人的帐户。
以下是第二个人创建帐户时两个日志中的示例:
开发
%%%%%%%%%%%%%%%%%%%%% account is = #<Account id: 97, name: "Eric Clapton", plan: "Partnership", billing_zip: nil, no_of_users: 1, no_of_assessments: nil, id_of_admin_user: 90, created_at: "2012-04-05 14:58:54", updated_at: "2012-04-05 14:58:56">
(0.2ms) BEGIN
SQL (1.5ms) INSERT INTO "assessments" ("account_id", "created_at", "description", "title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["account_id", 97], ["created_at", Thu, 05 Apr 2012 10:58:56 EDT -04:00], ["description", "Every day we bake fresh cakes to sell in our shop. This is our assessment of the risks involved in this process."], ["title", "Cake baking"], ["updated_at", Thu, 05 Apr 2012 10:58:56 EDT -04:00]]
生产
%%%%%%%%%%%%%%%%%%%%% account is = #<Account id: 2, name: "jack cale", plan: "Partnership", billing_zip: nil, no_of_users: 1, no_of_assessments: nil, id_of_admin_user: 2, created_at: "2012-04-05 14:46:54", updated_at: "2012-04-05 14:46:56">
(2.0ms) BEGIN
SQL (2.2ms) INSERT INTO "assessments" ("account_id", "created_at", "description", "title", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["account_id", 1], ["created_at", Thu, 05 Apr 2012 10:46:56 EDT -04:00], ["description", "Every day we bake fresh cakes to sell in our shop. This is our assessment of the risks involved in this process."], ["title", "Cake baking"], ["updated_at", Thu, 05 Apr 2012 10:46:56 EDT -04:00]]
更新
我在开发环境中将config.cache_classes
更改为true
,并且我现在收到完全相同的问题,所以它肯定与此有关。我使用这个gem将对象的创建范围仅限于当前帐户https://github.com/ErwinM/acts_as_tenant这可能是请求之间的缓存吗?
UPDATE2: 实际上就是这个。 current_tenant值正在会话之间缓存。我必须向accounts_controller添加一个方法,该方法在每次为新创建的帐户创建帐户时定义current_tenant值。