从 has_and_belongs_to_many 关系返回的对象不正确

发布于 2024-10-19 21:54:17 字数 1424 浏览 1 评论 0原文

我有两个模型,UserDiscussion

用户模型

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 技术交流群。

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

发布评论

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

评论(2

只为守护你 2024-10-26 21:54:17

我假设您的 Discussion 模型 belongs_to :user,这意味着讨论模型上的 user_id 字段将是该用户,它与has_and_belongs_to_many :subscribed_users 关联。

您可以发布您的模型和 schema.rb 吗?这将使您更容易理解您的模型正在做什么。

另外,我建议放弃 HABTM 并使用 has_many :through 代替,它会给你一个更丰富的模型。

I'm assuming your Discussion model belongs_to :user, which means the user_id field on the discussion model will be that user, it's unrelated to the has_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.

友欢 2024-10-26 21:54:17

我在 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.

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