从 has_and_belongs_to_many 关系返回的对象不正确
我有两个模型,User
和 Discussion
用户模型
has_and_belongs_to_many :subscribed_discussions, :class_name => 'Discussion', :join_table => 'discussions_subscriptions'
has_many :discussions
讨论模型
has_and_belongs_to_many :subscribed_users, :class_name => 'User', :join_table => 'discussions_subscriptions'
belongs_to :user
当我访问用户 u.subscribed_discussions[0].user_id
的讨论时,我与我直接从 Discussion 访问用户时相比,得到的 user
值完全不同。发现
system :029 > u.subscribed_discussions.first == Discussion.find(10)
=> true
system :030 > Discussion.find(10)
=> #<Discussion id: 10, title: "MBA", content: "What do you think about management education, speci...", user_id: 7, created_at: "2011-01-24 21:44:22", updated_at: "2011-01-24 21:44:22", group_id: nil, profile_id: 8>
system :031 > u.subscribed_discussions.first
=> #<Discussion id: 10, title: "MBA", content: "What do you think about management education, speci...", user_id: 1, created_at: "2011-01-24 21:44:22", updated_at: "2011-01-24 21:44:22", group_id: nil, profile_id: 8>
system :032 > u.id
=> 1
system :034 > Discussion.find(10).user_id
=> 7
u
是订阅讨论的用户id=10。当我通过 subscribed_discussions 关系访问讨论并访问其用户时,我得到的是 u
,而不是该讨论的创建者。
I have two models, User
and Discussion
User model
has_and_belongs_to_many :subscribed_discussions, :class_name => 'Discussion', :join_table => 'discussions_subscriptions'
has_many :discussions
Discussion model
has_and_belongs_to_many :subscribed_users, :class_name => 'User', :join_table => 'discussions_subscriptions'
belongs_to :user
When I access discussions from a user u.subscribed_discussions[0].user_id
, I get a completely different value of user
than when I access the user directly from Discussion.find
system :029 > u.subscribed_discussions.first == Discussion.find(10)
=> true
system :030 > Discussion.find(10)
=> #<Discussion id: 10, title: "MBA", content: "What do you think about management education, speci...", user_id: 7, created_at: "2011-01-24 21:44:22", updated_at: "2011-01-24 21:44:22", group_id: nil, profile_id: 8>
system :031 > u.subscribed_discussions.first
=> #<Discussion id: 10, title: "MBA", content: "What do you think about management education, speci...", user_id: 1, created_at: "2011-01-24 21:44:22", updated_at: "2011-01-24 21:44:22", group_id: nil, profile_id: 8>
system :032 > u.id
=> 1
system :034 > Discussion.find(10).user_id
=> 7
u
is a user who has subscribed to the discussion with id=10. When I access the discussion through the subscribed_discussions relationship, and access its user, I get u
back, instead of the creator of that discussion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您的
Discussion
模型belongs_to :user
,这意味着讨论模型上的user_id
字段将是该用户,它与has_and_belongs_to_many :subscribed_users
关联。您可以发布您的模型和 schema.rb 吗?这将使您更容易理解您的模型正在做什么。
另外,我建议放弃 HABTM 并使用
has_many :through
代替,它会给你一个更丰富的模型。I'm assuming your
Discussion
modelbelongs_to :user
, which means theuser_id
field on the discussion model will be that user, it's unrelated to thehas_and_belongs_to_many :subscribed_users
association.Can you post your models and schema.rb? It'll make it easier to understand what your model is doing.
Also, I suggest moving away from HABTM and using
has_many :through
instead, it'll give you a richer model.我在 IRC 上确认了这一点,如果表和连接表之一都有同名的列,那么这是 Rails 3 中 HABTM 关系中的一个错误。
I confirmed this on IRC that this is a bug in HABTM relationships in Rails 3, if one of the table and the join table both have a column of the same name.