remove duplicates from Relations


/ Published in: Rails
Save to your folder(s)

Remove all duplicate Adresses from first User.


Copy this code and paste it in your HTML
  1. #app/models/user.rb
  2. class User < ActiveRecord::Base
  3. has_many :adresses
  4. end
  5.  
  6. #app/models/adress.rb
  7. class Adress < ActiveRecord::Base
  8. belongs_to :user
  9. end
  10.  
  11. #irb
  12. User.first.adresses.map do |address|
  13. if liste.include? address
  14. address.destroy
  15. else
  16. liste.push address
  17. end
  18. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.