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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:23:32+00:00 2026-05-26T18:23:32+00:00

How would I use active record in this case: I have two tables: Users,

  • 0

How would I use active record in this case:

I have two tables: Users, Admins

Admins can be users also and thus have a user_id in their table.

I’d like to Select all users that DO NOT have a user_id set in the admins table

NOTE: This is just an example case. If this was a real case then of course I wouldn’t have two different tables for users and admins.

Edit: Again, I know the semantics of doing it like this is terrible db design — but it’s just an dummy case.

  • 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-26T18:23:33+00:00Added an answer on May 26, 2026 at 6:23 pm

    You can use :conditions to include a NOT IN:

    User.find(:all, :conditions => ['user_id not in (?)', @admins.map( &:id )])
    

    OR you can code it into a User’s scope ( a.k.a. named_scope prior to Rails 3.x ):

    class User < ActiveRecord::Base
      scope :not_admin, lambda { |admins| { :conditions => ['user_id not in (?)', admins.select( &:id ).join( ',' )] }
    end
    

    And use it as:

    User.not_admin( @admins )
    

    OR in order not to depend on @admins variable being passed you can use joins:

    class User < ActiveRecord::Base
      scope :not_admin, joins( :admin ).where( :admins => { :user_id => nil } )
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several active record models in a rails app and I would like
I would like to write a module that provides active record like functionality on
All, I have looked through source of Castle Active Record project (in .net) in
I have seen this problem arise in many different circumstances and would like to
I have a somewhat special use case, where I'd like to create a method
We allow users to add domains to an active record model such as User.domain
I would use a multi dimensional gaussian modell for regression. Rasmussen has a book
Why one would use one of the following packages instead of the other? Java
I thought I would use a stored routine to clean up some of my
In .NET I would use System.Diagnostics.Trace... What would I use in C or C++

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.