0

我有一个FamilyTree正在使用 PublicActivity 跟踪的课程。

关联如下:

用户

  has_one :family_tree, dependent: :destroy
  has_many :memberships, dependent: :destroy
  has_many :nodes, dependent: :destroy

节点

  include PublicActivity::Model
  tracked except: :update, owner: ->(controller, model) { controller && controller.current_user }
  tracked except: :update, recipient: ->(controller, model) { model && model.user }
  belongs_to :family_tree
  belongs_to :user

家谱

  include PublicActivity::Model
  tracked owner: ->(controller, model) { controller && controller.current_user }
  tracked recipient: ->(controller, model) { model && model.node.user }

  belongs_to :user
  has_many :memberships, dependent: :destroy
  has_many :members, through: :memberships, source: :user, dependent: :destroy
  has_many :nodes, dependent: :destroy

我的 FamilyTree 架构如下所示:

# == Schema Information
#
# Table name: family_trees
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  user_id    :integer
#  created_at :datetime
#  updated_at :datetime

我的节点架构如下所示:

# == Schema Information
#
# Table name: nodes
#
#  id             :integer          not null, primary key
#  name           :string(255)
#  family_tree_id :integer
#  user_id        :integer
#  media_id       :integer
#  media_type     :string(255)
#  created_at     :datetime
#  updated_at     :datetime
#  circa          :datetime
#  is_comment     :boolean

用户是正常的东西(电子邮件等)

我遇到的问题是在我的User课堂上,我有一个after_create :create_family_tree看起来像这样的问题:

def create_family_tree
  family_tree = self.build_family_tree(name: "#{name}'s Family Tree")
  family_tree.save!
end

但是当我创建一个新的用户帐户(即我注册为新用户)时,这是我得到的错误:

NoMethodError at /users
undefined method `node' for #<FamilyTree:0x007fcd3d3a9668> 

该错误发生在我的FamilyTree.rb模型中的这一行:

tracked recipient: ->(controller, model) { model && model.node.user }

以下是操作日志:

Started POST "/users" for 127.0.0.1 at 2015-02-24 19:35:13 -0500
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"E0pKO/ftHXNNUlM=", "user"=>{"name"=>" Def Jam", "email"=>"def@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create My App Account"}
Unpermitted parameters: name
   (1.1ms)  BEGIN
  User Exists (3.3ms)  SELECT  1 AS one FROM "users"  WHERE "users"."email" = 'def@test.com' LIMIT 1
  User Load (3.9ms)  SELECT  "users".* FROM "users"  WHERE "users"."confirmation_token" = 'asdadasdsac4ff'  ORDER BY "users"."id" ASC LIMIT 1
  SQL (3.8ms)  INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "created_at", "email", "encrypted_password", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["confirmation_sent_at", "2015-02-25 00:35:13.948623"], ["confirmation_token", "fdeb4f33c954f5ddae3c4ff"], ["created_at", "2015-02-25 00:35:13.942009"], ["email", "def@test.com"], ["encrypted_password", "$2a$10$t1aPhkOwS"], ["updated_at", "2015-02-25 00:35:13.942009"]]
  Rendered devise/mailer/confirmation_instructions.html.erb within layouts/mail (0.9ms)

DeviseMailer#confirmation_instructions: processed outbound mail in 54.2ms

Sent mail to def@test.com (49.5ms)
Date: Tue, 24 Feb 2015 19:35:14 -0500
From: support@myapp.com
Reply-To: support@myapp.com
To: def@test.com
Message-ID: <54ed18c24@myapp.local.mail>
Subject: My App App: Confirmation Instructions
</body>
</html>

  FamilyTree Load (2.9ms)  SELECT  "family_trees".* FROM "family_trees"  WHERE "family_trees"."user_id" = $1 LIMIT 1  [["user_id", 10]]
  SQL (3.3ms)  INSERT INTO "family_trees" ("created_at", "name", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", "2015-02-25 00:35:14.143397"], ["name", " 's Family Tree"], ["updated_at", "2015-02-25 00:35:14.143397"], ["user_id", 10]]
   (1.9ms)  ROLLBACK
Completed 500 Internal Server Error in 324ms

我认为一个潜在的解决方案可能是在创建root_nodefamily_tree时为family_tree创建一个(也就是after_create在family_tree上)。问题是请求永远不会到达那里,因为这个错误发生在family_tree 可以成功创建之前。

我不太确定如何进行。

有什么想法吗?

4

1 回答 1

0

那是因为您是build_family_tree在创建对象之前调用的。尝试after_commit改用。

after_commit :create_family_tree, on: :create
于 2016-01-05T02:55:32.313 回答