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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:28:41+00:00 2026-05-23T01:28:41+00:00

I have a table with data that needs to be updated at run-time by

  • 0

I have a table with data that needs to be updated at run-time by additional data from an external service. What I’d like to do is something like this:

MyModel.some_custom_scope.some_other_scope.enhance_with_external_data.each do |object|
  puts object.some_attribute_from_external_data_source
end

Even if I can’t use this exact syntax, I would like the end result to respect any scopes I may use. I’ve tried this:

def self.enhance_with_external_data
  external_data = get_external_data
  Enumerator.new do |yielder|
    # mimick some stuff I saw in ActiveRecord and don't quite understand:
    relation.to_a.each do |obj|
      update_obj_with_external_data(obj)
      yielder.yield(obj)
    end
  end
end

This mostly works, except it doesn’t respect any previous scopes that were applied, so if I do this:

MyModel.some_custom_scope.some_other_scope.enhance_with_external_data

It gives back ALL MyModels, not just the ones scoped by some_custom_scope and some_other_scope.

Hopefully what I’m trying to do makes sense. Anyone know how to do it, or am I trying to put a square peg in a round hole?

  • 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-23T01:28:42+00:00Added an answer on May 23, 2026 at 1:28 am

    I figured out a way to do this. Kind of ugly, but seems to work:

    def self.merge_with_extra_info
      the_scope = scoped
      class << the_scope
        alias :base_to_a :to_a
        def to_a
          MyModel.enhance(base_to_a)
        end
      end
      the_scope
    end
    
    def self.enhance(items)
      items.each do |item|
        item = add_extra_item_info(item)
      end
      items
    end
    

    What this does is add a class method to my model – which for reasons unknown to me seems to also make it available to ActiveRecord::Relation instances. It overrides, just for the current scope object, the to_a method that gets called to get the records. That lets me add extra info to each record before returning. So now I get all the chainability and everything like:

    MyModel.where(:some_attribute => some_value).merge_with_extra_info.limit(10).all
    

    I’d have liked to be able to get at it as it enumerates versus after it’s put into an array like here, but couldn’t figure out how to get that deep into AR/Arel.

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

Sidebar

Related Questions

I have a table of data that contains about 260 rows. Each of these
I have a map. On this map I want to show live data collected
So I have a typed dataset that I have created records for from another
I have 2 datetime fields in a single table. dt1 and dt2. dt1 has
I'm a little rushed for time so I know I could look for the
I am new to mySQL and would like some help with a query I'm
I can't seem to find an answer out there for this. Here's the scenario:
I've been looking at ranking algorithms recently, specifically those used by Reddit and Hacker
Although I couldn't find anything on it, I thought I would double check -
We are developing a multi-tenant application using NHibernate where all tenants share the same

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.