dimanche 10 janvier 2016

Using .select or .map to get an array of names from an activerecord model

I am trying to get an array of names from an activerecord model based on a search query.

I have this method in my item model.

def self.search(search)
    if search
      where(['lower(name) LIKE ?', "%#{search}%"])
    else
      Item.all
    end
  end

I am trying to figure out the difference between using these two lines they are both returning the same thing.

Item.search('ex').select('name').map(&:name) vs Item.search('ex').map(&:name)

Aucun commentaire:

Enregistrer un commentaire