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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:51:38+00:00 2026-05-27T23:51:38+00:00

This looks like it should be something pretty easy but I can’t seem to

  • 0

This looks like it should be something pretty easy but I can’t seem to get it to work. I have a model with a has_many relationship and I’d like a scope on the parent that allows me to select only certain attributes for each.

An example:

class Bakery < ActiveRecord::Base
  has_many :pastries
  scope :summary, select([:id, :name, 'some option calling pastries.summary'])

class Pastry < ActiveRecord::Base
  belongs_to :bakery
  scope :summary, select([:id, :image_url])

I’d like to be able to call something like Bakery.first.summary and get a Bakery model with only the id and name populated and for each pastry in it’s pastries array to only have the id and image_url attributes populated.

  • 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-27T23:51:39+00:00Added an answer on May 27, 2026 at 11:51 pm

    You could do this, but it won’t affect the SQL queries that are made as a result (assuming you’re trying to optimise the underlying query?):

    class Pastry
      ...
      def summary
        {
          :id => self.id,
          :image_url => self.image_url
        }
      end
    end
    
    class Bakery
      ...
      def summary     
        pastries.collect {|i| i.summary }
      end
    end
    

    This would then give you an array of hashes, not model instances.

    ActiveRecord doesn’t behave how you’re expecting with models – it will fetch whatever data it thinks you need. You could look at using the Sequel gem instead, or executing a raw SQL query such as:

    Pastry.find_by_sql("SELECT id, name from ...")
    

    But this could give you unexpected behaviour.

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

Sidebar

Related Questions

So I have a a text file that looks something like this: public >ret
This one seems like it should be pretty straightforward, but I couldn't quite figure
Something like Textile for the posts feels pretty necessary, but this is giving me
Does this look like it should work? I'm wanting to generate directions from one
This looks like a long shot, but does anyone know of a way to:
I want to know what this looks like. I don't have any ideas about
I have a table that looks like this: FOO BAR BAZ ----+----+---- foo1 bar1
I'm sure this is a pretty easy fix, but I'm not sure why my
This is not a homework. Visually it looks like a tree, but all leafs
I'm pretty new to R.. I'm reading in a file that looks like this:

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.