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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:03:18+00:00 2026-05-18T21:03:18+00:00

I am currently having a strange issue with an application migrated from rails 2.3.8

  • 0

I am currently having a strange issue with an application migrated from rails 2.3.8 to rails 3 (3.0.1, 3.0.2, 3.0.3).
At random moments associations behave strangely. In some cases, an associated object will return the Relation object, instead of the corresponding model. This seems to happen mostly on polymorphic associations. For example:

class A
  belongs_to :b, :polymorphic => true
end

class B
  has_many :a, :as => :source
end

When invoking “a.b” this will “sometimes” return the Relation object (causing a “undefined method … for ActiveRecord::Relation” error to raise) and some other times it will return the correct B object.
When the relation object is returned, it may sometimes be “fixed” temporarily by restarting the server, but it will eventually show up again.

Another issue i get is that when “getting” associated objects, sometimes the required filters are not automatically applied (where element id = …). this causes the query to return the first object in the table and not the correct associated object.

This is becoming a very frustrating issue, specially since i don’t seem to find anyone else with this or similar issues.

All finder methods in the application have been migrated to the new rails form, but this strange behavior remains.

The current configuration being used is:
Ubuntu 10
nginx server
passenger (3.0.2)
rails (3.0.3)
ruby 1.9.2p0 (2010-08-18 revision 29036)

  • 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-18T21:03:19+00:00Added an answer on May 18, 2026 at 9:03 pm

    After digging a bit deeper into the Active Record code, my co-worker and I found that the belongs_to_association.rb and belongs_to_polymorphic_association.rb were still using the old finder methods in the “find_target” method.

    We ran a couple tests by logging the resulting objects of this method from different queries and discovered the old finders were returning the ActiveRecord::Relation at random moments (loading the same object it would sometimes return the object and other times the Relation object).

    We overrode the find_target method for these 2 classes in initializer files, changing the finders used there to the new rails3 format (klass.select(…).where(…), etc). This seems to have solved the issue.

    The “has_many” association files are already using the new format, so these haven’t caused any issues.

    We applied these “fixes” to the rails 3.0.3 and hope that they will be resolved in future releases.

    Here are our initializer files in case someone else bumps into this problem:
    belongs_to_polymorphic_association.rb:

    #initializers/belongs_to_polymorphic_association.rb
    module ActiveRecord
      # = Active Record Belongs To Polymorphic Association
      module Associations
        class BelongsToPolymorphicAssociation < AssociationProxy #:nodoc:
          private
            def find_target
              return nil if association_class.nil?
    
              target =
                if @reflection.options[:conditions]
                  association_class.select(@reflection.options[:select]).where(conditions).where(:id => @owner[@reflection.primary_key_name]).includes(@reflection.options[:include]).first
                else
                  association_class.select(@reflection.options[:select]).where(:id => @owner[@reflection.primary_key_name]).includes(@reflection.options[:include]).first
                end
              set_inverse_instance(target, @owner)
    
              target
            end      
        end
      end
    end
    

    belongs_to_association.rb:

    #initializers/belongs_to_association.rb
    module ActiveRecord
      # = Active Record Belongs To Associations
      module Associations
        class BelongsToAssociation < AssociationProxy #:nodoc:
    
          private
            def find_target
              key_column = (@reflection.options[:primary_key]) ? @reflection.options[:primary_key].to_sym : :id
    
              options = @reflection.options.dup
              (options.keys - [:select, :include, :readonly]).each do |key|
                options.delete key
              end
              options[:conditions] = conditions
    
              the_target= @reflection.klass.select(options[:select]).where(key_column => @owner[@reflection.primary_key_name]).where(options[:conditions]).includes(options[:include]).readonly(options[:readonly]).order(options[:order]).first if @owner[@reflection.primary_key_name]
              set_inverse_instance(the_target, @owner)
              the_target
            end
    
        end
      end
    end
    

    Hope this is useful for someome

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

Sidebar

Related Questions

I'm currently having a major issue with a python script. The script runs arbitrary
I am currently having some problems with <li> in IE6 essentially my code looks
Hopefully someone can shed a little light on an issue that I'm currently having
I'm having an issue with a query that currently uses LEFT JOIN weblog_data AS
We're currently having a debate whether it's better to throw faults over a WCF
I am currently having a hardtime understanding and implementing events in C# using delagates.
I'm currently having a problem with a ShoppingCart for my customer. He wants to
I'm currently adding verbose tooltips to our site, and I'd like (without having to
I am having a strange problem when using this XML-to-NSDictionary class.really it was working
I'm having a very strange problem that may be the result of my ignorance

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.