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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:58:00+00:00 2026-05-26T05:58:00+00:00

Is it possible to set an instance-level constraint on a has_many, :through relationship in

  • 0

Is it possible to set an instance-level constraint on a has_many, :through relationship in rails 3.1?

http://guides.rubyonrails.org/association_basics.html#the-has_many-association

Something like:

Class A
    has_many :c, :through => :b, :conditions => { "'c'.something_id" => @a.something_id }

The documentation gives me hope with this, but it doesn’t work for me:

If you need to evaluate conditions dynamically at runtime, you could
use string interpolation in single quotes:

class Customer < ActiveRecord::Base
  has_many :latest_orders, :class_name => "Order",
    :conditions => 'orders.created_at > #{10.hours.ago.to_s(:db).inspect}'
end

That gives me “unrecognized token ‘#'” on rails 3.1. Wondering if this functionality doesn’t work anymore?

EDIT

Want to clarify why I don’t think scopes are the solution. I want to be able to get from an instance of A all of the Cs that have a condition (which is based on an attribute of that instance of A). These are the only Cs that should EVER be associated with that A. To do this with scopes, I would put a scope on C that takes an argument, and then have to call it from @a with some value? I don’t get why that’s better than incorporating it into my has_many query directly.

  • 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-26T05:58:01+00:00Added an answer on May 26, 2026 at 5:58 am

    Use a scope on the orders model:

    class Order < ActiveRecord::Base
      belongs_to :customer
      scope :latest, lambda { where('created_at > ?', 10.hours.ago) }
    end
    

    And then call it with:

    @customer.orders.latest
    

    And if you really want to use latest_orders, you can instead add this to the Customer model:

    def latest_orders
      orders.where('created_at > ?', 10.hours.ago)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to set the cursor to 'wait' on the entire html page
Is it possible to set the data in a ContentPlaceHolder through code? This is
Is it possible to set the parent of the class? I.e. an instance of
Quick question: Is it possible to set the delegate of an AVAudioPlayer instance to
Is it possible to set an instance name for a MC in its script?
Is it possible to get/set instance variables of an object from inside a module
Is it possible to set up a Rails application such that all controller actions
In SQL Server 2005, is it possible to automatically set transaction isolation level to,
In Java SE it is possible set the cause of an exception using initCause
Is possible to set an image as an iPhone application unique icon in code

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.