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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:42:07+00:00 2026-05-25T06:42:07+00:00

I’m trying to show only brand instances which the current user has not tagged,

  • 0

I’m trying to show only brand instances which the current user has not tagged, even if other users have tagged the same brand already. Something like:

enter image description here

Controller

This is my controller code and even though it should be working, it currently returns all brand instances.

@brand = current_user.brands.includes(:taggings).where( [ "taggings.id IS NULL OR taggings.tagger_id != ?", current_user.id ] ).order("RANDOM()").first

Schema (including my join model for good measure)

create_table "brand_users", :force => true do |t|
t.integer  "brand_id"
t.integer  "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "taggings", :force => true do |t|
t.integer  "tag_id"
t.integer  "taggable_id"
t.string   "taggable_type"
t.integer  "tagger_id"
t.string   "tagger_type"
t.string   "context"
t.datetime "created_at"
end

add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"

create_table "tags", :force => true do |t|
t.string "name"
end

end
  • 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-25T06:42:07+00:00Added an answer on May 25, 2026 at 6:42 am

    So if you’re using the acts-as-taggable-on gem and have the following models:

    class User < ActiveRecord::Base
      acts_as_tagger
      has_many :brand_users
      has_many :brands, :through => :brand_users
    end
    

    So you also have the tables in your schema like:

    create_table "users", :force => true  do |t|
      t.string "name"
    end
    
    create_table "brands", :force => true  do |t|
      t.string "name"
    end
    

    Then the following SQL query should hopefully do what you want (?):

    SELECT brands.*
    FROM brands
    WHERE brands.id NOT IN (
        SELECT brands.id
        FROM brands
        INNER JOIN brand_users ON brand_users.brand_id = brands.id
        INNER JOIN taggings ON (taggings.tagger_id = brand_users.user_id AND taggings.tagger_type = 'User')
        WHERE brand_users.user_id = 1 AND taggings.taggable_id = brand_users.brand_id
    )
    

    To translate this into Rails ORM, I can’t get any closer without hard coding the whole sub-select SQL string, something like:

    class Brand < ActiveRecord::Base
      has_many :brand_users
      has_many :users, :through => :brand_users
    
      scope :has_not_been_tagged_by_user, lambda {|user| where("brands.id NOT IN (SELECT brands.id
        FROM brands
        INNER JOIN brand_users ON brand_users.brand_id = brands.id
        INNER JOIN taggings ON (taggings.tagger_id = brand_users.user_id AND taggings.tagger_type = 'User')
        WHERE brand_users.user_id = ? AND taggings.taggable_id = brand_users.brand_id)", user.id) }
    
    end
    

    (I know you could do this and then use ruby’s .map(&:id).join(‘,’) but if this is a large app I think you loose a lot of performance by taking this out of the database, converting it into a string of integers and feeding it back in (as I understand it).)

    Then in your controller I think you’d do something like:

    @brand = current_user.brands.has_not_been_tagged_by_user(current_user)

    As an aside, I think this would actually then execute an SQL like below (is that right?):

    SELECT brands.*
    FROM users
    INNER JOIN brand_users ON brand_users.user_id = users.id
    INNER JOIN brands ON brands.id = brand_users.brand_id 
    WHERE brands.id NOT IN (
        SELECT brands.id
        FROM brands
        INNER JOIN brand_users ON brand_users.brand_id = brands.id
        INNER JOIN taggings ON (taggings.tagger_id = brand_users.user_id AND taggings.tagger_type = 'User')
        WHERE brand_users.user_id = 1 AND taggings.taggable_id = brand_users.brand_id
    ) AND users.id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.