创建新帐户时,我有类似于以下的代码作为 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
to true
from false
,现在我遇到了完全相同的问题,所以它肯定与此有关。我正在使用此 gem 将对象的创建范围仅限于当前帐户https://github.com/ErwinM/acts_as_tenant这可能是请求之间缓存的内容吗?
UPDATE2: 实际上正是这样。current_tenant 值在会话之间被缓存。每次为新创建的帐户创建帐户时,我都必须向 accounts_controller 添加一个定义 current_tenant 值的方法。