Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Using extending within a scope block is senstitive to the ordering of scope methods #12022

Closed
ahacking opened this Issue Aug 26, 2013 · 8 comments

Comments

Projects
None yet
6 participants

The following scope block fails when methods in the extension are used:

has_many :comments,
    -> {
      extending CommentsExtension
      order :updated_at
    }

Any call to a method, eg foo defined in the CommentsExtension module fails with

NoMethodError - undefined method `foo' for #<ActiveRecord::Associations::CollectionProxy []>:
  activerecord (4.0.0) lib/active_record/relation/delegation.rb:121:in `method_missing'
  activerecord (4.0.0) lib/active_record/relation/delegation.rb:68:in `method_missing'
  activerecord-deprecated_finders (1.0.3) lib/active_record/deprecated_finders/collection_proxy.rb:22:in `method_missing'

Changing the order as follows and things now work as expected.

 has_many :comments,
    -> {
      order :updated_at
      extending CommentsExtension
    }

Here is a test that fails, flip the order of the extending and order and it passes:

# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.0'
require 'active_record'
require 'minitest/autorun'
require 'logger'

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts do |t|
  end

  create_table :comments do |t|
    t.integer :post_id
  end
end

module CommentExtensions
  def foo
    "foo"
  end
end

class Post < ActiveRecord::Base
  has_many :comments,
    -> {
      extending CommentExtensions
      order :id
   }
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class BugTest < MiniTest::Unit::TestCase
  def test_association_stuff
    post = Post.create!
    assert_equal "foo", post.comments.foo
  end
end
Member

robin850 commented Aug 26, 2013

This bug is present aon 4.0.0, 4-0-stable and master and I can also confirm that it works on each of these versions when we permute the order of each method.

Maybe you should try this:

  has_many :comments, -> { extending(CommentExtensions).order(:id) }

Calling the two methods in different lines without chaining them will return only the last one, ignoring the first, so in the first example the association only knows about order, in the "working" example it only knows about extending.

Thanks for the info @carlosantoniodasilva

It is not obvious from the documentation that this is in fact the intended behavior. Calling extending on a scope to inject methods into the association proxy? Really???

The docs don't even say the scope block has to be the second argument, and I've burnt a lot of time on poor documentation bugs like this in AR, we shouldn't have to live our lives on stack overflow discovering how to get basics like ordering on a collection done ESPECIALLY when AR 4.0 breaks existing code.

If that's how it should behave that is fine, however the docs need to be_much_ clearer and the issue should remain open until those with the secret knowledge step up and fix the documentation bugs and let the community know exactly what the intended behavior is.

@ahacking ahacking added the stale label Apr 23, 2014

Owner

rafaelfranca commented May 1, 2014

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 team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

ahacking commented May 1, 2014

I don't use AR anymore, so I'm unable to help sorry. Sequel has proved to be a much better option.

Feel free to close or leave open as a documentation issue. Left open perhaps someone will be aware that the docs need some attention and maybe someone else who uses AR can contribute, closed the problem remains hidden but your issues list appears shorter. I would choose the latter but hey, its not my project.

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this May 27, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment