2
votes

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.

2
I think maybe you need to post more details of your models, like the has_many statements of Goal, Role, User, Accountkek
Do you have something which is not covered here: dl.dropbox.com/u/766146/ruby-on-rails-data-relationships.pngAmala

2 Answers

0
votes

You don't need to explicitly set it you should just be able to do;

user.goals

as long as you have the following in your model:

has_many :goals, :through=>:joining_table_name
0
votes

Since you did not post the details of the models, I'm guessing the following based on the error:

User has_one Account
User has_many Goals
Goal belongs_to User
Goal has_one Account

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 NOT has_one Account