Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:45:13+00:00 2026-06-14T12:45:13+00:00

High guys. This isn’t behaving the way I think it should which means I’m

  • 0

High guys. This isn’t behaving the way I think it should which means I’m doing it wrong;

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :properties
end

class Property < ActiveRecord::Base
  has_and_belongs_to_many :tags

  def amenities
    tags.where(:classification => :amenity)
  end
end

So I have Properties and Tags. They have a HABTM relationship with a pivot table.

When I do a .tags on a property, I get the full list and if I do a .clear on that full list it correctly removes the associations from the database.

When I do a .amenities I get only those tags that are flagged with the classification of amenity correctly, but if I do a .clear on those results it fails to remove them but rather just does the .amenities query again in the console with an output of [].

So this means it’s just .clear‘ing the result array.. not the association which is what I actually want.

So the question then is; what is the correct way to .clear an association from a HABTM relationship while giving it essentially a where clause to limit which associations are being removed?

Thanks guys. Hope that wasn’t too confusing..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:45:15+00:00Added an answer on June 14, 2026 at 12:45 pm

    Instead of defining a method querying tags, you could add another tag association with conditions, like:

    class Property < ActiveRecord::Base
      has_and_belongs_to_many :tags
    
      # this will be just like the tags association, except narrow the results
      # to only tags with the classification of 'amenity'
      has_and_belongs_to_many :amenities, 
                              :class_name => 'Tag', 
                              :conditions => { :classification => 'amenity' }
    
    end
    

    clear, and any other habtm assocation methods, should work as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The high level problem we're having is this. We've got Apache Wave installed and
I've searched high and low in order to solve this problem, but I can't
I've searched high and low for the answer to this, but I can't figure
High Guys, I have to define a polymorphic datatype for a tree that can
Hey guys, I have this great scripts someone on stack overflow helped me out
Hi guys I remove /page from high_voltage gem with this answer. Remove page/ of
I've searched high and low for a solution to this, with no luck. My
So part of my web app centers around this very important form which is
I'm trying to ask this question in a non-subjective way. The question is pretty
High Level With StructureMap, Can I define a assembly scan rule that for an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.