Last active
August 29, 2015 14:01
-
-
Save mrded/a6814f7c45707b945c28 to your computer and use it in GitHub Desktop.
Rails: array of IDs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you want to go directly to the DB | |
ActiveRecord::Base.connection.select_values("select id from users") | |
# If you already have a model | |
User.all(:select => :id).collect(&:id) | |
# In Rails 3 you can do this | |
User.pluck(:id) | |
# Rails helper | |
User.comment_ids | |
# Without DB | |
User.all.collect { |obj| obj.id } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment