[katello-devel] New call Justin added to AR

Partha Aji paji at redhat.com
Wed Aug 15 23:05:16 UTC 2012


Many may not know this, so just highlighting a new call Just Sherrill added to katello master. Its called "pluck"

Often we just need one attribute of an AR object, for example if we just need the list of ids of environments or pulp_ids of a Repository, we'd write something like this

KTEnvironment.select(:id).where(<...>).collect {|env| env.id}
Repository.select(:id).where(<...>).collect {|r| r.pulp_id}

One common mistake here is people forget to add the "select" so you end up loading all columns even though you just need id or pulp_id

With Justin's magic method (which is in newer versions of AR. He back ported it) one can now do

KTEnvironment.where(<...>).pluck(:id)
Repository.where(<...>).pluck(:pulp_id)

This magic method makes sure you are only selecting what you need.


Partha




More information about the katello-devel mailing list