I’m trying to use has_many :through, since my join model deserves being
[…]
If I reload my page, user.colors.length == 2 again, and the old color is
still a part of the collection.
I think there is a problem in your table relations.
:through: Specifies a Join Model to perform the query through. Options
for :class_name and :foreign_key are ignored, as the association uses
the source reflection. You can only use a :through query through a
belongs_to or has_many association.
:through: Specifies a Join Model to perform the query through. Options
for :class_name and :foreign_key are ignored, as the association uses
the source reflection. You can only use a :through query through a
belongs_to or has_many association.
Have you noticed the last sentence?
I think I’m doing it right.
class User
has_many :favorites
has_many :colors, :through => :favorites
end
class Color
has_many :favorites
has_many :users, :through => :favorites
end
class Favorite
belongs_to :user
belongs_to :color
end
Are you saying I’ve still got the relationships wrong somehow?
def delete_color
color = Color.find(params[:color_id])
user.colors.delete(color) # this works in script/console as expected
at this point, user.colors.length is correct
redirect :action => :show # this will show 2 favorite colors again:
user.colors.length is wrong
end
Can you have a look in the log?
I’m interested in what SQL commands are executed on this save if only
colors association changed and no other user data.
Does it only update the association to colors or everything from user
model?
Thanks
Markus
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.