has_and_belongs_to_many 关系不双向关联
我使用 has_and_belongs_to_many 设置关系来关联用户和事件。然后我尝试这个:)
user = User.find(1)
event = Event.find(1
这
两个当前都没有关联...然后我尝试通过执行以下操作来关联它们:
user.events << event
此操作有效...但是,它们彼此没有正确关联:
user.events 正确列出了事件对于此用户...但 event.users 没有与其关联的用户。
我该如何做到这一点,以便当我将一个与另一个关联时(事件与用户或用户与事件)......它会自动以另一种方式关联?
i setup a relationship using has_and_belongs_to_many to associate users and events. Then I try this:
user = User.find(1)
event = Event.find(1
)
both of these are not currently associated...then I try to associate them by doing:
user.events << event
this action works...however, they don't associate correctly for each other:
user.events lists the event correctly for this user...but event.users does not have that user associated with it.
how do I make it so that when I associate one with the other (either the event with user or user with event)...it automatically associates the other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的链接设置正确,您可以对此进行测试:
这将显式地从数据库重新加载数据,而不是使用本地缓存的版本。如果您曾经访问过该关联,它将不再在数据库中查找它,除非您明确询问它。
这有帮助吗?
Assuming your links are setup correctly, you can test this:
This will explicitly reload the data from the database, instead of using the locally cached version. If you have ever accessed the association, it will not look it up in the database anymore, unless you explicitly ask it.
Does that help?
has_and_belongs_to_many
是否存在于两个模型中?听起来好像不是,但它 应该是:Is the
has_and_belongs_to_many
present in both models? It sounds like it is not, whereas it should be: