Active Record 有没有办法到达那里
我有一个用户模型,其中有多个角色,并且该角色有许多用户,所有这些都通过中间表进行。最后角色 has_many/has_one 目标。我想通过用户达到目标,或者让我说我想做一些类似 User has_many :goals through :roles 的事情。当我这样做并尝试设置 user.goals = Goal.all 时。它给出了以下异常。
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection:无法修改关联“User#goals”,因为源反射类“Goal”通过 :has_one 关联到“Account”。
有什么办法可以实现这一点吗?我在 RailsAPI 上没有看到类似的东西。
I have a User model which has_many Roles and the Role has many users, all through an intermediate table. At the end Role has_many/has_one goal. I want to reach goals through user or let me say I want to do something like User has_many :goals through :roles. When I do it and try to set user.goals = Goal.all. It gives following exception.
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'User#goals' because the source reflection class 'Goal' is associated to 'Account' via :has_one.
Is there any way to achieve this. I don't see anything like that on RailsAPI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要显式设置它,您应该能够做到;
只要您的模型中有以下内容:
You don't need to explicitly set it you should just be able to do;
as long as you have the following in your model:
由于您没有发布模型的详细信息,我根据错误猜测以下内容:
如果是这种情况,那么您将通过将所有目标关联到一个用户来破坏此模型,因此目标应该有
has_many
而非has_one
帐户Since you did not post the details of the models, I'm guessing the following based on the error:
If that is the case, then you're breaking this model by associating all goals to one user, and therefore goal should have
has_many
and NOThas_one
Account