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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:45:05+00:00 2026-05-16T18:45:05+00:00

I have the following models to associate users with roles: class User < ActiveRecord::Base

  • 0

I have the following models to associate users with roles:

class User < ActiveRecord::Base
  has_many :user_role_assignments, :dependent => :destroy
  has_many :user_roles, :through => :user_role_assignments
end

class UserRole < ActiveRecord::Base
  has_many :user_role_assignments
  has_many :users, :through => :user_role_assignments
end

class UserRoleAssignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :user_role

  named_scope :has_admin_role, 
    :joins => :user_role, 
    :conditions => "user_roles.name = 'admin'",
    :group => "user_role_assignments.user_id" 

  named_scope :has_non_admin_role, 
    :joins => :user_role, 
    :conditions => "user_roles.name <> 'admin'",
    :group => "user_role_assignments.user_id" 
end

The named scopes in UserRoleAssignment work in SQLite, but not in postgreSQL, which issues this error:

ActiveRecord::StatementInvalid: PGError: ERROR:  column "user_role_assignments.id" must appear in the GROUP BY clause or be used in an aggregate function
: SELECT "user_role_assignments".* FROM "user_role_assignments"   INNER JOIN "user_roles" ON "user_roles".id = "user_role_assignments".user_role_id  WHERE (user_roles.name = 'admin')  GROUP BY user_role_assignments.user_id

I have tried many different variations of the named scope (adding a :having clause, adding additional groupings) but each time I add something, a new error arises in postgreSQL so I have reverted back to my original, simple named_scopes. The point of the first scope is to retrieve all users that either have a role with name “admin” and the second is to retrieve users that have some role other than “admin”. I use this with searchlogic from other models and it works as expected in SQLite, but not postgreSQL (e.g. User.user_role_assignments_has_admin_role).

How can I modify these named_scopes to work with postgreSQL?

  • 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-16T18:45:06+00:00Added an answer on May 16, 2026 at 6:45 pm

    Postgres is much more picky about GROUP BY clauses than sqlite3.

    You’ll have to add all of the user_role_assignments column names to your GROUP BY clause:

    class UserRoleAssignment < ActiveRecord::Base
      belongs_to :user
      belongs_to :user_role
    
      named_scope :has_admin_role, 
        :joins => :user_role, 
        :conditions => "user_roles.name = 'admin'",
        :group => "user_role_assignments.id, user_role_assignments.user_id, user_role_assignments.user_role_id" 
    
      named_scope :has_non_admin_role, 
        :joins => :user_role, 
        :conditions => "user_roles.name <> 'admin'",
        :group => "user_role_assignments.id, user_role_assignments.user_id, user_role_assignments.user_role_id" 
    end
    

    If that doesn’t work, you can try this next (but I’m not sure if it will work).

    class UserRoleAssignment < ActiveRecord::Base
      belongs_to :user
      belongs_to :user_role
    
      named_scope :has_admin_role, 
        :joins => :user_role, 
        :conditions => "user_roles.name = 'admin'",
        :group => "user_role_assignments.*" 
    
      named_scope :has_non_admin_role, 
        :joins => :user_role, 
        :conditions => "user_roles.name <> 'admin'",
        :group => "user_role_assignments.*" 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models: class Mark < ActiveRecord::Base validates_presence_of :user validates_presence_of :page belongs_to
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
I defined these 3 models in Rails3. class User < ActiveRecord::Base has_many :questions has_many
I have a following model class Order < ActiveRecord::Base has_many :products, :through => :line_items
I have four models in my application, defined as follows class User < ActiveRecord::Base
I am in rails 3.1. I have the following models class Tool < ActiveRecord::Base
I have 2 models (Book & Image) class Book < ActiveRecord::Base has_many :images accepts_nested_attributes_for
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
I am using Rails 3.2. I have following models: Blog Comment User class Blog

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.