创建后的变量

发布于 2024-11-09 02:26:57 字数 1697 浏览 0 评论 0原文

我目前有以下内容:

class Question < ActiveRecord::Base
  belongs_to :product
  belongs_to :user
  belongs_to :asker, :class => "User", :foreign_key => "asker_id"
  has_one :track, :as => :trackable

  after_create :make_track

  def make_track
    create_track
  end
end

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :questions
  has_one :track, :as => :trackable

  after_create :make_track

  def make_track
    create_track
  end
end

class Track < ActiveRecord::Base
  belongs_to :user
  belongs_to :trackable, :polymorphic => true
end

我想做的是获取属于用户(User)的曲目(Track),以便获得:

u = User.first
t = u.tracks

我尝试放入:

  def make_track
    create_track(:user_id => :user)
  end

  def make_track
    create_track(:user_id => :user_id)
  end

但它没有帮助。如何在创建后插入保存当前问题/产品的用户的 user_id?

跟随 mu 后,下面的答案太短了。我得到的输出是这样的:

  SQL (0.1ms)   BEGIN
  Question Create (0.2ms)   INSERT INTO `questions` (`votes`, `created_at`, `asker_id`, `product_id`, `updated_at`, `question`) VALUES(0, '2011-05-23 07:55:25', 1, 1, '2011-05-23 07:55:25', 'the eight')
  Track Load (0.4ms)   SELECT * FROM `tracks` WHERE (`tracks`.trackable_id = 13 AND `tracks`.trackable_type = 'Question') LIMIT 1
  Track Columns (1.3ms)   SHOW FIELDS FROM `tracks`
  Track Create (0.3ms)   INSERT INTO `tracks` (`trackable_id`, `created_at`, `trackable_type`, `updated_at`, `user_id`) VALUES(13, '2011-05-23 07:55:25', 'Question', '2011-05-23 07:55:25', NULL)
  SQL (54.6ms)   COMMIT

我尝试在 after_create 中输出 self.user ,结果是 nil

I currently have the following:

class Question < ActiveRecord::Base
  belongs_to :product
  belongs_to :user
  belongs_to :asker, :class => "User", :foreign_key => "asker_id"
  has_one :track, :as => :trackable

  after_create :make_track

  def make_track
    create_track
  end
end

class Product < ActiveRecord::Base
  belongs_to :user
  has_many :questions
  has_one :track, :as => :trackable

  after_create :make_track

  def make_track
    create_track
  end
end

class Track < ActiveRecord::Base
  belongs_to :user
  belongs_to :trackable, :polymorphic => true
end

What I want to do is get the tracks(Track) that belongs_to a user(User) in order to get:

u = User.first
t = u.tracks

I tried putting in:

  def make_track
    create_track(:user_id => :user)
  end

  def make_track
    create_track(:user_id => :user_id)
  end

but it doesn't help. How do I insert the user_id of the User that saved the current question/product in the after create?

After following mu is too short's answer below. I got the output which is this:

  SQL (0.1ms)   BEGIN
  Question Create (0.2ms)   INSERT INTO `questions` (`votes`, `created_at`, `asker_id`, `product_id`, `updated_at`, `question`) VALUES(0, '2011-05-23 07:55:25', 1, 1, '2011-05-23 07:55:25', 'the eight')
  Track Load (0.4ms)   SELECT * FROM `tracks` WHERE (`tracks`.trackable_id = 13 AND `tracks`.trackable_type = 'Question') LIMIT 1
  Track Columns (1.3ms)   SHOW FIELDS FROM `tracks`
  Track Create (0.3ms)   INSERT INTO `tracks` (`trackable_id`, `created_at`, `trackable_type`, `updated_at`, `user_id`) VALUES(13, '2011-05-23 07:55:25', 'Question', '2011-05-23 07:55:25', NULL)
  SQL (54.6ms)   COMMIT

I tried outputting self.user in after_create which resulted in nil.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情感失落者 2024-11-16 02:26:57

您只需在 self 上调用适当的访问器方法:

def make_track
  create_track(:user_id => self.user)
end

You just call the appropriate accessor method on self:

def make_track
  create_track(:user_id => self.user)
end
花开半夏魅人心 2024-11-16 02:26:57

您发布的 SQL 表明问题在于用户没有设置问题(这就是为什么在传递给 create_track 时它为 nil)。

问题真的应该直接属于用户,还是通过产品属于用户?

The SQL you posted suggests that the problem is that the user is not being set on the question (which is why it is nil when passing to create_track).

Should Question really belong directly to User, or does it belong to User through Product?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文