创建后的变量
我目前有以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需在
self
上调用适当的访问器方法:You just call the appropriate accessor method on
self
:您发布的 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?