Method: ActiveRecord::Calculations#ids
- Defined in:
- lib/active_record/relation/calculations.rb
#ids ⇒ Object
Returns the base model’s ID’s for the relation using the table’s primary key
Person.ids # SELECT people.id FROM people
Person.joins(:companies).ids # SELECT people.id FROM people INNER JOIN companies ON companies.id = people.company_id
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/active_record/relation/calculations.rb', line 361 def ids primary_key_array = Array(primary_key) if loaded? result = records.map do |record| if primary_key_array.one? record._read_attribute(primary_key_array.first) else primary_key_array.map { |column| record._read_attribute(column) } end end return @async ? Promise::Complete.new(result) : result end if has_include?(primary_key) relation = apply_join_dependency.group(*primary_key_array) return relation.ids end columns = arel_columns(primary_key_array) relation = spawn relation.select_values = columns result = if relation.where_clause.contradiction? ActiveRecord::Result.empty else skip_query_cache_if_necessary do klass.connection.select_all(relation, "#{klass.name} Ids", async: @async) end end result.then { |result| type_cast_pluck_values(result, columns) } end |