User with attributes id, name, and age. What is the output of this code?User.where(age: 30).pluck(:name)
User.where(age: 30).pluck(:name)pluck extracts specific column values as an array.The where filters users by age 30, and pluck(:name) returns an array of their names only, not full objects or hashes.
select and pluck together?where filters first, then select limits columns, and pluck extracts the column values. Option B follows this order correctly.
User.pluck(:name).where(age: 30)
Why does it raise an error?
User.pluck(:name).where(age: 30)pluck returns and what methods are available on it.pluck returns a plain Ruby array, which does not have ActiveRecord query methods like where. So calling where after pluck causes a NoMethodError.
User.where(active: true).select(:id, :name).pluck(:name)
What does it return?
User.where(active: true).select(:id, :name).pluck(:name)
pluck returns after selecting columns.The where filters active users, select limits columns but pluck(:name) extracts only the names as an array of strings.
pluck over select when fetching specific columns?pluck be more efficient than select?pluck directly retrieves the specified columns as arrays from the database, skipping the creation of ActiveRecord objects, which makes it faster and uses less memory.
