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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:46:33+00:00 2026-06-17T02:46:33+00:00

quick rails section, say I have two models Users and Roles and I want

  • 0

quick rails section, say I have two models Users and Roles and I want to create an index/list of Users based on a certain Role, how do I go about building that in my controller

is it something like

#first create the association
@user = role.build

#then build the index based on a Role of role_id = 2
@userrole = @user.where(@user.role_id == 2)

I know this is pseudo code, but is this correct? And what is the proper rails code?

  • 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-06-17T02:46:34+00:00Added an answer on June 17, 2026 at 2:46 am
    roles = Role.where(id: [1, 2])
    users = User.where(role_ids: roles.collect(&:ids))
    

    This is just an example because obviously if we already know the ids we wouldn’t need the first query but if our roles had an editable column (as an example) we could do:

    roles = Role.where(editable: true)
    users = User.where(role_ids: roles.collect(&:ids))
    

    And if we wanted to order these we could simply add:

    users = User.where(role_ids: roles.collect(&:ids)).order("created_at DESC")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a quick question, I have 2 models as follows, when I create
Quick question: I have been learning ruby on rails and http://guides.rubyonrails.org/index.html is really good
Quick question, I have the following string which is a coma separated list of
Quick method description: I have two tables, lets name them table ONE and table
I am just doing a quick scaffold on rails, and I'm going to have
Just starting with Rails. I have a quick question that uses Paperclip and S3
I'm trying to run a quick rake task on all my Rails models but
I have a roles model in a rails app that I have written a
I want to have Greek support in my rails app for the messages/flashes etc.
Is there a way to generate a scaffold for Rails models that have either

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.