Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveRecord::Base#ids - Close #5812 #6091

Merged
merged 1 commit into from Apr 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -139,6 +139,16 @@ def pluck(column_name)
end
end

# Pluck all the ID's for the relation using the table's primary key
#
# Examples:
#
# Person.ids # SELECT people.id FROM people
# Person.joins(:companies).ids # SELECT people.id FROM PEOPLE INNER JOIN companies ON companies.person_id = people.id
def ids
pluck primary_key
end

private

def perform_calculation(operation, column_name, options = {})
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Expand Up @@ -465,4 +465,8 @@ def test_pluck_not_auto_table_name_prefix_if_column_joined
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
assert_equal [7], Company.joins(:contracts).pluck(:developer_id)
end

def test_plucks_with_ids
assert_equal Company.all.map(&:id), Company.ids
end
end