This project is archived and is in readonly mode.

#6218 ✓stale
Gerardo

Rails 3 random associations behavior returning ActiveRecord::Relation

Reported by Gerardo | December 23rd, 2010 @ 09:18 PM

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)

Comments and changes to this ticket

  • Gerardo

    Gerardo December 24th, 2010 @ 08:41 PM

    After digging a bit deeper into the Active Record code, my coworker 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.

    Attached are our initializer files in case someone else bumps into this problem.

  • rails

    rails March 25th, 2011 @ 12:00 AM

    • Tag changed from activerecord relation, problem, rails3.0.3 to activerecord relation, problem, rails303
    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails March 25th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages