Deleting only first occurrence from many to many collection

Hello,
I am having a little bit problem in deleting duplicate entry of my many
to
many tables.

In many to many fields delete method is deleting all the occurrence of
collection. Say I have (demo example):

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.

Users.cars.delete(car) #deletes all occurrence of car
Users.cars.delete_at(User.cars.find_index(video_card)) #delete_at does
not exist

Now how to resolve this?

I have also posted at stackoverslow (
http://stackoverflow.com/questions/31507056/deleting-first-occurrence-from-many-to-many-collection-entry-at-rails).
Please feel free to answer, and/or upvote.

Thanks.

Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)
www.sadafnoor.com

Don’t know what your skill level is, but I would suggest checking out
RailsCasts, #47 Two Many-to-Many:
http://railscasts.com/episodes/47-two-many-to-many
Your join table could be called CarOwners, as a point relevant to the
RailsCast… Check it out… The general idea is that it is maybe
better
if you use :has_many :through…

Hope this helps.

On 20 July 2015 at 00:53, Sadaf N. [email protected] wrote:

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]

How can a user have the same car multiple times, it doesn’t make sense.

Colin

@colin, certainly I can have a same model car twice :stuck_out_tongue: whatever, it was
just a made up scenario.
@Elizabeth, railscast helped. I did it with has_many :through. :slight_smile:

Thank you everyone.

2015-07-21 3:02 GMT+06:00 Elizabeth McGurty [email protected]:

You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected]
To post to this group, send email to [email protected]
To view this discussion on the web visit

https://groups.google.com/d/msgid/rubyonrails-talk/d33d14b9-c9b4-4bb5-9edf-4c9d79554824%40googlegroups.com

https://groups.google.com/d/msgid/rubyonrails-talk/d33d14b9-c9b4-4bb5-9edf-4c9d79554824%40googlegroups.com?utm_medium=email&utm_source=footer

.
For more options, visit https://groups.google.com/d/optout.


Md. Sadaf N. (@sadaf2605 https://twitter.com/sadaf2605)
www.sadafnoor.com

On 21 July 2015 at 10:33, Colin L. [email protected] wrote:

what you have done already.
Or possibly even better
User has_many cars
CarModel has_many cars

So the join model means something in the real world.

Colin

On 21 July 2015 at 10:27, Sadaf N. [email protected] wrote:

@colin, certainly I can have a same model car twice :stuck_out_tongue: whatever, it was
just a made up scenario.

If it is the model of car (not an actual car) then you should call the
model CarModel or similar to avoid confusion. In that case I agree with
Elizabeth, use has_many through. Also you might like to consider having
a
counter in the join table rather than multiple joins, or perhaps that is
what you have done already.

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs