0

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?

4
  • Which version of AR is this? Commented Aug 27, 2013 at 23:24
  • Well I hadn't installed the gem yet, so I guess just 4.0 Commented Aug 27, 2013 at 23:25
  • And is hostname an attribute of the Host model? Edit: I forgot you're not in Rails. Commented Aug 27, 2013 at 23:25
  • Umm like I said, I'm not really familiar with ActiveRecord. I have a class called Host and it extends ActiveRecord::Base, but I didn't know I needed to do more. It should be a column in Host though. Commented Aug 27, 2013 at 23:27

2 Answers 2

1
Host.where(:realm=>"Stage",:status=>"UP")
Sign up to request clarification or add additional context in comments.

4 Comments

will this give me the hostname?
it will give the entire row, you can do Host.where(:realm=>"Stage",:status=>"UP").first().hostname if you want the hostname of the first item.
Ok, so what if I just want an array of all the hostnames?
use pluck(:hostname) instead of first().hostname
1
Host.where(:realm => 'stage', :status => 'UP').pluck(:hostname)

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

1 Comment

Ok, I'm having trouble connecting to the database right now, but when I do, I think this is the answer that will work so I'll mark this one correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.