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

  • Home
  • SEARCH
  • 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 963819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:43:44+00:00 2026-05-16T01:43:44+00:00

To set the stage, I’m using rails 3 and I have these tables and

  • 0

To set the stage, I’m using rails 3 and I have these tables and relationships:

user has_many lists
list has_many tasks
task has_many stints

I would like to build a query that allows me to select all of the current users stints, and to have the list.id available as an attribute on each stint in the result. I would need to rename list.id to list_id because otherwise it overwrites the id attribute of the stint in the result. I figured “as” would work, but it doesn’t.

This gives me the last stint belonging to user 1:

Stint.joins(:task => [{:list => :user }]).where(:lists => {:user_id =>  1 }).last
 => #<Stint id: 753, task_id: 245> 

What I would like however, is:

 => #<Stint id: 753, task_id: 245, list_id: 2>

So I figured this would work:

Stint.joins(:task => [{:list => :user }]).where(:lists => {:user_id =>  1 }).select('stints.*, lists.id as list_id').last
=> #<Stint id: 753, task_id: 245> 

As you can see, no difference. But if I don’t use “as”, I get this:

Stint.joins(:task => [{:list => :user }]).where(:lists => {:user_id =>  1 }).select('stints.*, lists.id').last
=> #<Stint id: 2, task_id: 245> 

The list.id is used, but because the attribute is set with the name “id” it hides the stint.id.

Any help would be appreciated.

EDIT – The SQL is fine. The query returns the correct columns and values, including list_id.
The problem is that it doesn’t create an attribute on the model.

  • 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-16T01:43:44+00:00Added an answer on May 16, 2026 at 1:43 am

    The attribute was there all along. It just wasn’t being displayed in the result.

    s = Stint.joins(:task => [{:list => :user }]).where(:lists => {:user_id =>  1 }).select('stints.*, lists.id as list_id').last
    => #<Stint id: 753, task_id: 245>
    
    s.attributes
    => {"id"=>753, "list_id"=>"2", "task_id"=>245} 
    
    s.list_id
    => "2" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following animation set up using Three.js: http://frontier.lincoln.ac.uk/3d/development/Stage2/characters/man2.html Please ignore the ropey
Let's set the stage, PHP & MYSQL: I have a table we'll call Directions
I have a square movieclip on stage. I am trying to set the dimensions
I have a Ruby on Rails 3.2 app using bundler and capistrano for deployment.
I have set up debugging in Eclipse for native code using the Android NDK
Lets set the stage Doing a windows phone app, and trying to authenticate using
I want to store a cookie using something like Response.Cookies.Set(new HttpCookie(name,value); after I have
I have an iFrame on a page set to display: none; <div id=the-stage style=display:none;>
i can set the stage's background color, etc., using a SWF metadata tag: [SWF(width=1024,
Well, i have to set the stage frame rate according to the frame rate

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.