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 7537689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:55:09+00:00 2026-05-30T06:55:09+00:00

We have the following relationship for our data: Users Have and belong to many

  • 0

We have the following relationship for our data:

Users

  • Have and belong to many Pages
  • Have and belong to many Posts

Pages

  • Have and belong to many Users
  • Have and belong to many Tags

Posts

  • Have and belong to many Users
  • Have and belong to many Tags

Tags

  • Have and belong to many Pages
  • Have and belong to many Posts

Here is our question:
Is there a way to get just a count of how many unique Users have ever created either a Page or Post that has been tagged with a given Tag?

In our Tag model:

function getSupportingUserCount($tag_id)
{
    $user_count = $this->find(?);
    return $user_count;
}
  • 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-30T06:55:10+00:00Added an answer on May 30, 2026 at 6:55 am

    You can just avoid joins that may be slow if you’ve got many rows. So first get the tagid, then search for that id via the User model associations.

    $tag = $this->Tag->find('first', array('conditions'  => array('Tag.name' => 'blahblah'), 'recursive'  => -1);
    $tag_id = $tag['Tag']['id'];
    $this->User->find('count', array('conditions'  => "OR" => array (
        array('Page.tag_id'  => $tag_id,
              'Post.tag_id'  => $tag_id )
    ));
    

    (untested code so there may be typos)

    (aside: are you sure each Post has many users? usually a post has only 1 author)

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

Sidebar

Related Questions

I have the following relationship CType has many Field Field has many Ctype and
I have following relationship hierarchy between core-data entities (managed objects): Entity1 > Entity2 >
In my application I have the following relationship: Document has_and_belongs_to_many Users User has_and_belongs_to_many Documents
I have the following many-to-many relationship: class Assignment < ActiveRecord::Base belongs_to :programmer belongs_to :project
What's the right way to do this: I have the following relationship between entities
I have the following relationship defined in Core Data Person --> Worked <-- Job
I have the following relationship: Post hasMany Comment Post belongsTo Category Post HABTM Tag
If I have the following master-detail relationship: owner_tbl auto_tbl --------- -------- owner ---> owner
I have the following domains: User, Role, Company. User and Role has m:n relationship,
I'm working on my first Rails project, and I have the following model relationship:

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.