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

  • Home
  • SEARCH
  • 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 6102795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:39:20+00:00 2026-05-23T13:39:20+00:00

I am looking for an alternative way to check if ActivityType has any relationships

  • 0

I am looking for an alternative way to check if ActivityType has any relationships as the way I am doing it now is too costly (activity_type.activities.empty?).

I thought about creating a new method such as ActivityType#linked_to_activity? which would do a query on ActivitiesActivityTypes HABTM join table. This is faster than the previously mentioned method but it is still too slow.

Any pointers are appreciated (apart from the usual “HABTM should be replaced by HM:through” non-constructive comment)

Models

class Activity < ActiveRecord::Base
  has_and_belongs_to_many :activity_types, :uniq => true
end

class ActivityType < ActiveRecord::Base
  has_and_belongs_to_many :activities
end

View

%table
  - @activity_types.each do |activity_type|
    %tr
      %td
        - if activity_type.activities.empty?
          Do something here...

Attempt at ActivityType#linked_to_activity?

class ActivityType < ActiveRecord::Base
  has_and_belongs_to_many :activities

  def linked_to_activity?
    ActivityType.find_by_sql(["SELECT * FROM activities_activity_types WHERE activity_type_id = ?", id]).empty? ? false : true
  end
end

NB:

  • Running on Rails v2.3.5
  • DB is MySQL with indices on foreign keys
  • 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-05-23T13:39:21+00:00Added an answer on May 23, 2026 at 1:39 pm

    I think you are on the right track. It’s a matter of preference, but I usually do it with a count:

    class ActivityType < ActiveRecord::Base
      has_and_belongs_to_many :activities
    
      def linked_to_activity?
        self.activities.count > 0
      end
    end
    

    Edit:

    As the comment below states: Using select count(*) will only retrieve one line from the DB, while select * retrieves all (unless of course you add a limit 1 at the end).

    Also: I try not to litter my models with plain SQL unless it’s absolutely necessary for performance reasons.

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

Sidebar

Related Questions

I was looking for a way to check if a program is installed using
Is there any alternative way to implement a switch case in Java other than
I am looking for an alternative way asides using NSTask/system() to run launchctl load
I'm looking for an alternative way to get the query parameter names from an
I am looking for a relatively quick way to check whether words are misspelled,
I am looking for alternative SOAP ( javax.xml.soap ) implementation, other than Sun SAAJ
http://github.com/gabriel/yajl-objc I've already tried SBJSON, and while it works, I'm looking into alternative options
I am looking for an alternative to using an object/variable in global scope --
I am looking for an alternative to acts_as_solr or thinkingsphinx for fulltext search in
I am looking for an alternative to C-style union. boost::variant is one such option.

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.