A method on association proxy sometimes caches old target #1419

Closed
amatsuda opened this Issue May 31, 2011 · 3 comments

Projects

None yet

2 participants

Member

On ActiveRecord 3.0.x, a method defined on association proxy illegally caches outdated target, only when the method calls the target's scope.

Here's an example.

class Author < ActiveRecord::Base
  has_many :books do
    def newest
      # calling Book's scope
      published.last
    end
  end
end

class Book < ActiveRecord::Base
  belongs_to :author
  scope :published  # any kind of scope
end
a = Author.first
a.books.create :title => 'book 1'
#=> #<Book id: 1, author_id: 1, title: "book 1">
# you see the scope works
a.books.newest
#=> #<Book id: 1, author_id: 1, title: "book 1">

# create a new book
a.books.create :title => 'book 2'
#=> #<Book id: 2, author_id: 1, title: "book 2">
# "newest" still returns the old model
a.books.newest
#=> #<Book id: 1, author_id: 1, title: "book 1">
# correctly returns the newest one when reloaded
a.books.reload
a.books.newest
#=> #<Book id: 2, author_id: 1, title: "book 2">

I confirmed this happens on 3.0.0..3.0.8.rc4, and does NOT happen on master (3.1.rc1) and 2.3.11 (changing "scope => named_scope").

Member
sikachu commented Jun 1, 2011

This seems like a valid regression for 3.0.x. Do you want to make a patch for this?

Member
amatsuda commented Jun 2, 2011

I'm afraid I have no time to dig into this complex problem ATM, so your (or anyone's) patch would be appreciated :)

@sikachu sikachu was assigned Jun 2, 2011
@arunagw arunagw added a commit to arunagw/rails that referenced this issue Jun 3, 2011
@arunagw arunagw Test added for namedscope target. #1419 66fb568
@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011
@aaronbatalion @josh aaronbatalion + josh Fixed asset host to not cache objects [#1419 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
d7f4921
Member
sikachu commented Apr 25, 2012

@amatsuda I don't think we're going to release another 3.0.x release, so I'm closing this.

@sikachu sikachu closed this Apr 25, 2012
@heathd heathd referenced this issue in alphagov/whitehall Jan 29, 2015
@tekin tekin Remove redundant ActiveRcord bug workaround code
The workaround for the ActiveRecord bug is no longer required. In fact, it
appears to have the opposite effect now! Reworded the regression test to
hopefully make it clearer what it is covering.
5beb396
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment