# Sql query in rails

**URL:** <https://www.ruby-forum.com/t/sql-query-in-rails/127093>\
**Category:** Rails\
**Created:** [January 24, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/sql-query-in-rails/127093 "2008-01-24T22:57:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [January 24, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/sql-query-in-rails/127093/1 "2008-01-24T22:57:59Z")

</div>

Hello,  
Can anyone please tell me how do i write a sql query in rails in which  
only one field in not null and the others are null.

FOr e.g. i want to find all records in which email is not null and all  
the other fields are null. I do not want to mention the null fields  
explicitly in query because there are many of them.

Thank you.

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [January 24, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/sql-query-in-rails/127093/2 "2008-01-24T23:25:37Z")

</div>

Ank Ag wrote:

> Hello,  
> Can anyone please tell me how do i write a sql query in rails in which  
> only one field in not null and the others are null.
> 
> FOr e.g. i want to find all records in which email is not null and all  
> the other fields are null. I do not want to mention the null fields  
> explicitly in query because there are many of them.
> 
> Thank you.  
> not sure of the best way, here is one though:

imagine your object is called Business

@business = Business.find\_by\_sql([‘SELECT \* FROM business WHERE email !=  
null AND column2 = null AND column3 = null AND column4 = null’])

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001377)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.
