0

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?

0

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

About rest of the question I bet there was meaning value = ...SQL query... and then if value.nil?...
that returned me all row from table. But need to get only one value from the column.
Use the pluck method in combination with first (see updated answer)
great! That's it! Tnx
Also: CompanySetting.where(company_id: self.id, title:'Vacation days').first.value
0

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

"Do Something" if val.nil?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.