In many to many fields delete
method is deleting all the occurrence of collection. Say I have:
class user < ActiveRecord::Base
has_and_belongs_to_many :cars
end
class car < ActiveRecord::Base
has_and_belongs_to_many :users
end
users and cars are many to many relationship, I have defined my users_cars
table. Now user can have repetitive car entry as relation. For example:
Car: A,B,C User: U1,U2,U3
U1=[A,B,C,A,A,A,B]
Which can be implemented using many to many relationship, the way I have implemented. BUT, at the time when I want to delete one of the car entries of user the problem occurs.
User.cars.delete(car) #deletes all occurrence of car
User.cars.delete_at(User.cars.find_index(video_card)) #delete_at does not exist
Now how to resolve this?
User.cars.delete_at(User.cars.find_index(car))
withoutUser.cars.delete(car)
?User.cars.delete(car)
deletes all the occurrences. DoesUser.cars.delete_at(User.cars.find_index(card))
also deletes all the occurrences?User.cars
?cars
method on class level?