snowe
snowe

Reputation: 1375

Selecting all rows based off of multiple columns using ActiveRecord

I'm trying to use activerecord to work with a mysql database, but not in Rails. I'm not at all familiar with databases or ActiveRecord, this is just a short query I need to grab an array of the hostnames of servers from a database. I need to grab all hostnames where the Realm column equals "Stage" and the Status column equals "UP". The database is named ops and the table is host.

I've looked up ActiveRecord queries and I think that I need to do something like

Host.all(:select => "hostname", :conditions => ["realm=stage", "status=UP"])

but this seems wrong.

Can anyone help with this?

Upvotes: 0

Views: 64

Answers (2)

Marcelo De Polli
Marcelo De Polli

Reputation: 29281

Host.where(:realm => 'stage', :status => 'UP').pluck(:hostname)

Pluck will give you an array of hostnames, which is what I think you want here.

Upvotes: 1

shaish
shaish

Reputation: 1499

Host.where(:realm=>"Stage",:status=>"UP")

Upvotes: 1

Related Questions