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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:10:00+00:00 2026-05-23T22:10:00+00:00

In my Rails models I have: class Song < ActiveRecord::Base has_many :flags has_many :accounts,

  • 0

In my Rails models I have:

class Song < ActiveRecord::Base
  has_many :flags
  has_many :accounts, :through => :flags
end

class Account < ActiveRecord::Base
  has_many :flags
  has_many :songs, :through => :flags
end

class Flag < ActiveRecord::Base
  belongs_to :song
  belongs_to :account
end

I’m looking for a way to create a scope in the Song model that fetches songs that DO NOT have a given account associated with it.

I’ve tried:

Song.joins(:accounts).where('account_id != ?', @an_account)

but it returns an empty set. This might be because there are songs that have no accounts attached to it? I’m not sure, but really struggling with this one.

Update

The result set I’m looking for includes songs that do not have a given account associated with it. This includes songs that have no flags.

Thanks for looking.

  • 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-23T22:10:00+00:00Added an answer on May 23, 2026 at 10:10 pm

    Am I understanding your question correctly – you want Songs that are not associated with a particular account?

    Try:

    Song.joins(:accounts).where(Account.arel_table[:id].not_eq(@an_account.id))
    

    Answer revised: (in response to clarification in the comments)

    You probably want SQL conditions like this:

    Song.all(:conditions =>
      ["songs.id NOT IN (SELECT f.song_id FROM flags f WHERE f.account_id = ?)", @an_account.id]
    )
    

    Or in ARel, you could get the same SQL generated like this:

    songs = Song.arel_table
    flags = Flag.arel_table
    
    Song.where(songs[:id].not_in(
      flags.project(:song_id).where(flags[:account_id].eq(@an_account.id))
    ))
    

    I generally prefer ARel, and I prefer it in this case too.

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

Sidebar

Related Questions

I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I have two models Horse and Race. class Horse < ActiveRecord::Base has_many :races end
I have the following three models (Rails 2.3.8) class Outbreak < ActiveRecord::Base has_many :incidents,
I have 2 models class User < ActiveRecord::Base has_many :products end class Product <
I'm using Rails 3. I have 3 models: class Deal < ActiveRecord::Base has_many :wishes,
I have two models: class Member < ActiveRecord::Base has_many :member_tags end and class MemberTag
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models class Collection < ActiveRecord::Base has_many :presentations has_many :galleries, :through =>
I have my models setup like so: class User < ActiveRecord::Base has_many :posts, :foreign_key
ruby 1.9.2p290 rails 3.1.1 I have these models: class Recipe < ActiveRecord::Base belongs_to :festivity

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.