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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:47:44+00:00 2026-05-28T05:47:44+00:00

Lets say I have two Rails models, Users and Lists. Users have_many lists and

  • 0

Lets say I have two Rails models, Users and Lists. Users have_many lists and lists belong_to a user.

Users also have one particular list they are working on at any given time. This is denoted by the active_list_id foreign key on the User model.

Let’s say I want to run the equivalent of the following query via ActiveRecord:

SELECT * FROM users JOIN lists 
ON lists.id = users.active_list_id

Basically I am trying to write a method which will get each user, along with her currently active list, to display statistics on list completion.

The solution seems to me to be something like

Users.joins(:lists).where("users.active_list_id  = lists.id") 

But I can’t seem to get that to work**, and I’m not sure what the most idiomatically correct solution is in Rails.

How should I pull this data most efficiently and effectively using ActiveRecord?

Thanks in advance for any help you can offer.

EDIT: The above query does work, but does not seem to pull a conjunction of the user record and its associated list.

  • 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-28T05:47:44+00:00Added an answer on May 28, 2026 at 5:47 am

    There are some strangr things on your post. First thing is: “have_many” thats wrong its called has_many that might be a problem if its in your model like you wrote it down! Then the other thing is the name of the foreign key! The foreign key by convention is the name of the association in singular with addition of _id. When you say you want to hasv user.lists the foreign key must be called list_id and not active_list_id! You can specify an not conventional foreign key by passing the foreign_key option in the association! That would look like this:

    User.rb:

    has_many :lists, :foreign_key => 'active_list_id'
    

    List.rb:

    belongs_to :user, :foreign_key => 'active_list_id'
    

    Then you would be able to say it like this:

    User.find(<id>).lists
    

    You NEVER EVER join tables in rails using SQL! Dont try to transfer PHP knowlage to RoR 1 to 1, just dont!

    // Ahh now I understand what you ceed is a scope with parameters!
    User.rb:

    scope :active_list, lambda{|list_id| where(:active_list_id =>  list_id)}
    

    Then you could call User.active_list(<list_id>) to get all the users using this list!

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

Sidebar

Related Questions

I am using Rails 3.0.8 Lets say that I have two collections of users.
lets say i have two drop down list and one button on my search
Lets say I have two tables - child and parent with many-to-one relation. What
Lets say I have data in two tables. In one I have Order ID
Lets say I have a function f[x_, y_] , and two lists l1 ,
Let's say I have two models, Classes and People. A Class might have one
Lets say I have two tables, one for transactions, and another table who's primary
Lets say we have two databases, one named db1, the other one named db2.
I have a question about efficient implementation. Lets say I have two arrays: One
Lets say I have two branches in RepoX called BranchA and BranchB. RepoX also

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.