Anu11
Anu11

Reputation: 316

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

Upvotes: 3

Views: 200

Answers (1)

Stone
Stone

Reputation: 2668

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.

Upvotes: 1

Related Questions