Mary Dear
Mary Dear

Reputation: 185

SQL-query to ActiveRecord-query. Ruby

I need to write this SQL-query on ruby

SELECT value FROM CompanySetting WHERE company_id = 1 AND title = 'Work days'

and next get this value for the check, like this

value = ...SQL query... and then if value.nil?..

Can you help me write method which do this, please?

Upvotes: 0

Views: 139

Answers (2)

santosh
santosh

Reputation: 1611

val=CompanySetting.where(company_id: 1, title: "Work days").pluck(:value).first

"Do Something" if val.nil?

Upvotes: 0

Christian-G
Christian-G

Reputation: 2361

Assuming you have a model CompanySetting, you can use the following to obtain the record with your conditions:

CompanySetting.where(company_id: 1, title: "Work days").pluck(:value)

If you just want the first record use:

CompanySetting.where(company_id: 1, title: "Work days").pluck(:value).first

If you want to test if a value is nil, you can use value.nil?

I suggest you read some documentation

Upvotes: 1

Related Questions