Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

AR::Relation#model would be a better API than AR::Relation#klass #6606

Merged
merged 1 commit into from

4 participants

@amatsuda
Collaborator

model feels more natural for programmers, and this would make library authors a bit happier since it's compatible with DM::Collection
https://github.com/datamapper/dm-core/blob/9d9f739/lib/dm-core/collection.rb#L45

@parndt

This would be nice :-)

@rafaelfranca
Owner

Agree. I would change the internals to use model instead of klass too, and make klass and alias of model. But I don't know if we can change the @klass too.

cc/ @jonleighton @tenderlove

@alexeymuranov

+1, i also propose in my PR #4367 to rename set_fixture_class to �set_fixture_model.

@rafaelfranca rafaelfranca merged commit d168c1f into from
@sayap sayap referenced this pull request from a commit in sayap/will_paginate
@sayap sayap AR: Do not spawn additional queries when rendering page_entries_info.
`collection.first.class` will spawn a LIMIT 1 query, while `collection.empty?`
will spawn a COUNT query. The latter is not a problem since a COUNT query is
needed anyway, so redundant ones will retrieve from cache. The former, however,
can add unnecessary load to the database.

As far as I can tell, ActiveRecord::Relation has a `klass` attribute since the
beginning in 0e0866e0565bd530ee265b9298accff4185f7022, so it should work fine.
Since rails/rails#6606, the attribute is also aliased
as `model`, though this change is only available in Rails 4.x.
d19e220
@sayap sayap referenced this pull request from a commit in sayap/will_paginate
@sayap sayap AR: Do not spawn additional queries when rendering page_entries_info.
`collection.first.class` will spawn a LIMIT 1 query, while `collection.empty?`
will spawn a COUNT query. The latter is not a problem since a COUNT query is
needed anyway, so redundant ones will retrieve from cache. The former, however,
can add unnecessary load to the database.

As far as I can tell, ActiveRecord::Relation has a `klass` attribute since the
beginning, so it should be fine. The attribute is also aliased as `model` since
rails/rails#6606, though this change is only available
in Rails 4.x.
9076eb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
1  activerecord/lib/active_record/relation.rb
@@ -20,6 +20,7 @@ class Relation
attr_reader :table, :klass, :loaded
attr_accessor :default_scoped
+ alias :model :klass
alias :loaded? :loaded
alias :default_scoped? :default_scoped
View
5 activerecord/test/cases/relation_test.rb
@@ -19,6 +19,11 @@ def test_construction
assert !relation.loaded, 'relation is not loaded'
end
+ def test_responds_to_model_and_returns_klass
+ relation = Relation.new :a, :b
+ assert_equal :a, relation.model
+ end
+
def test_initialize_single_values
relation = Relation.new :a, :b
(Relation::SINGLE_VALUE_METHODS - [:create_with]).each do |method|
Something went wrong with that request. Please try again.