How to Avoid Duplicates in a Has_Many :Through Relationship

how to avoid duplicates in a has_many :through relationship?

What about:

Blog.find(:all,
:conditions => ['id NOT IN (?)', the_reader.blog_ids])

Rails takes care of the collection of ids for us with association methods! :)

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Laravel Eloquent has many through variation, remove duplicates

Not sure if this can be done in Eloquent builder. Maybe something like this:

public function orders(): BelongsToMany {
return $this->belongsToMany(
Order::class,
'invoice_line',
'invoice_id',
'orderid'
)->distinct();
}

But you can also do:

// class Invoice
public function getRelatedOrders ()
{
$orderIdList = $this->invoiceLines->pluck('order_id')->unique();
return Order::whereIn('id', $orderIdList)->get();
{

Rails idiom to avoid duplicates in has_many :through

As long as the appended role is an ActiveRecord object, what you are doing:

user.roles << role

Should de-duplicate automatically for :has_many associations.

For has_many :through, try:

class User
has_many :roles, :through => :user_roles do
def <<(new_item)
super( Array(new_item) - proxy_association.owner.roles )
end
end
end

if super doesn't work, you may need to set up an alias_method_chain.

How to avoid creating duplicate records in join table with has_many through association?

I found out a workaround by placing accepts_nested_attributes_for. My associations are now like this:

class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
accepts_nested_attributes_for :appointments
end

class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end

class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
accepts_nested_attributes_for :patients
end

And i did this in the console:

a = Physician.last
a.update(appointment_attributes: [{appointment_data: DateTime.now, patient_attributes: {name: 'Rajesh', disease: 'fever'}}])

It is now creating only one record in the appointments table.

rails, accepts_nested and has_many :through is creating duplicate entries

This is a confirmed bug in Rails, with a fix set to be included in 2.3.6.

https://rails.lighthouseapp.com/projects/8994/tickets/3575-multiple-join-records-when-using-nested_attributes-in-habtm

Eloquent save many to many relation without duplicates

You can use firstOrCreate() to avoid adding already existing tags into the database.

foreach ( $tags_r as $tag ) {
$tags[] = Tag::firstOrCreate(['slug' => str_slug($tag)]);
}

$p->tags()->saveMany( $tags );

I also used str_slug() instead of manually creating the slug.

Rails preventing duplicates in polymorphic has_many :through associations

You could do it a couple of ways.

Define a custom add_tags method that loads all the existing tags then checks for and only adds the new ones.

Example:

def add_tags *new_tags
new_tags = new_tags.first if tags[0].kind_of? Enumerable #deal with Array as first argument
new_tags.delete_if do |new_tag|
self.tags.any? {|tag| tag.name == new_tag.name}
end
self.tags += new_tags
end

You could also use a before_save filter to ensure that the list of tags doesn't have any duplicates. This would incur a little more overhead because it would happen on EVERY save.

SQL Server - delete duplicate rows of a table that has many-to-many relationship

Maybe delete the duplicate rows first, like this:

DELETE 
A
FROM TABLEA A
INNER JOIN
(
SELECT *,
RANK() OVER(PARTITION BY name, surname
ORDER BY ID_A) rank
FROM TABLEA
) T ON A.ID_A = t.ID_A
WHERE rank > 1;

And then delete rows in your matrix table that no longer exist in Table A.

DELETE FROM TABLEB WHERE ID_A NOT IN(SELECT ID_A FROM TABLEA)

(Note the delete statement may be off syntax-wise as I am typing from phone!)



Related Topics



Leave a reply



Submit