Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to remove duplicate records using active record query

I have two models Venue and Venue_type.Each Venue has many venue types like

stadium,Bar,Hall,Uncategorized etc.I want to delete all the rows from the venue table which

is listed in one of these categories and also listed under uncategorized.Basically I dont

want that particular venue to appear in uncategorized label as it already has a category.I

am using rails 2.3.4.I tried this but its not working:

uncategorized = VenueType.get_by_label("Uncategorized")

vs.each{|v| puts v in v.venue_types.size>1 and v.venue_types.collect(&:id}.include(uncategorized.id)}

v.destroy!

These are the two models:

class Venue < ActiveRecord::Base
has_and_belongs_to_many:venue_types
end

class Venuetype < ActiveRecord::Base
has_and_belongs_to_many:venues
end
like image 445
Anu11 Avatar asked Dec 07 '25 13:12

Anu11


1 Answers

You haven't given me enough info to fix everything for you - but I can tell you that you're not using a Ruby block correctly because you're trying to access a block instance variable outside of the block. You don't have scope for that.

Here's "syntax-wise" what would work:

uncategorized = VenueType.get_by_label("Uncategorized")

vs.each do |v| 
     if (v in v.venue_types.size>1 and v.venue_types.collect(&:id}.include(uncategorized.id))
          v.destroy!
     end
end

albeit I can't give your an exact answer because I can't see all the code.

like image 81
Stone Avatar answered Dec 10 '25 02:12

Stone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!