this is a real hair-puller. I’ve got a junction table that I need to query for 2 different entries, then update the junction table if the values are not there already.
Essentially I have this:
master_actors = [#<Actor2role actorId: 13176, dogTag: 45917, roleId: 1, position: 3>, #<Actor2role actorId: 65471, dogTag: 45917, roleId: 291075, position: 1>]
slave_actors = [#<Actor2role actorId: 11123, dogTag: 5384, roleId: 44, position: 5>, #<Actor2role actorId: 65471, dogTag: 5384, roleId: 291075, position: 0>, #<Actor2role actorId: 66652, dogTag: 5384, roleId: 291073, position: 2>]
I need to compare the contents of each one of those records and if there are entries in slave_actors not present in master_actors I need insert them.
What I can’t figure out is how to compare the contents of those 2 activeRecord relationship objects. Regular array methods like .include? don’t seem to work. Nor does master_actors.attributes == slave_actors.attributes because I get an error saying that the attributes method does not exist for an activeRecord relationship.
Basically I’m really stumped.
Any help would be appreciated.
Have you tried checking if the intersection (
ary1 & ary2) is empty ?EDIT : As mentioned in the comments below, since there is elements in
master_actorsthat can be absent fromslave_actors, the difference (ary1 - ary2) was the right answer