# empty field check.

**URL:** <https://discuss.rubyonrails.org/t/empty-field-check/63576>\
**Category:** rubyonrails-talk\
**Created:** [April 13, 2012, 11:01pm UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576 "2012-04-13T23:01:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 13, 2012, 11:01pm UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576/1 "2012-04-13T23:01:04Z")

</div>

Hi, I'm trying to select record as follows: @course = CourseDate.find\_all\_by\_date\_and\_canceled\_time(Date.today,nil)

records with todays date but those where the canceled\_time is empty, the above find selects all records for today also those records where the canceled\_time is not empty.

What is wrong here ?

Thanks for any hint.

Dani

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Avello](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Juan\_Pablo\_Avello](https://discuss.rubyonrails.org/u/Juan_Pablo_Avello)\
**Post date:** [April 14, 2012, 10:06am UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576/2 "2012-04-14T10:06:54Z")

</div>

If you are using Rails 3+, you’d probably get better results with CourseDate.where(:date =\> Date.today, :canceled\_time =\> nil)

This brings out an ActiveRecord::Relation object which you can iterate through as if it was an Array, but to which you can keep concatenating scopes for further filtering.

I believe ‘where’ is not available in Rails 2, but then you may use CourseDate.find(:conditions =\> “…”)

Use of scopes is highly advisable in my experience. With your same example, you may have 2 scopes in your model as well:

- ‘available’ which checks canceled\_time is not null

- ‘today’ which checks date is today (or one receiving a date, which would be even more useful)

This way you can just make a call looking like: “CourseDate.available.today” or "“CourseDate.available.for\_date(Date.today)”, which I guess you might agree its highly descriptive and has a clear meaning on what it does.

If all this doesn’t work you, check those attributes’ types and assure they have the expected values on db.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [April 14, 2012, 10:17am UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576/3 "2012-04-14T10:17:17Z")

</div>

> Hi, I'm trying to select record as follows: @course = CourseDate.find\_all\_by\_date\_and\_canceled\_time(Date.today,nil)
> 
> records with todays date but those where the canceled\_time is empty, the above find selects all records for today also those records where the canceled\_time is not empty.

Have a look in log/development.log and see what query is being run.

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 14, 2012, 11:12am UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576/4 "2012-04-14T11:12:19Z")

</div>

Thank you Juan and Colin. One thing I didn't mention is that canceled\_time comes from different class where the relationship is:

class CourseDate \< ActiveRecord::Base &nbsp;&nbsp;has\_many :course\_lessons end

CourseLesson class CourseLesson \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :course\_date end

so in the query: @course = CourseDate.find\_all\_by\_date\_and\_canceled\_time(Date.today,nil)

'date' is in table 'course\_dates' 'canceled\_time' is in table 'course\_lessons'

So the suggested: CourseDate.where(:date =\> Date.today, :canceled\_time =\> nil) did not work (thanks Juan)

But I'm getting all records for today (and their related lessons), eventhogh one of the records has its 'canceled\_time' set with time. I'm of course expecting to get only those records with 'canceled\_time' not set.

I'm using rails 3+

Colin, scope names looks elegant and I'll use it, but first I would like to get my original query working.

Any hints how ?

Thanks

Dani

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Avello](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Juan\_Pablo\_Avello](https://discuss.rubyonrails.org/u/Juan_Pablo_Avello)\
**Post date:** [April 14, 2012, 11:40am UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576/5 "2012-04-14T11:40:21Z")

</div>

Certainly, that changes things a lot; find\_by\_\* or where() constructed like this, expect all attributes used to be on the same table. If you need data from another table, you must join those tables in sql, so you need to add includes() or join() to your method chain.

Using ‘where’, your query should look like:

CourseDate.includes(:course\_lessons).where(:date =\> Date.today, :course\_lessons =\> { :cancelled\_time =\> nil})

or:

CourseDate.includes(:course\_lessons).where(“course\_dates.date = ? and course\_lessons.cancelled\_time is null”, Date.today)

Regards.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 14, 2012, 12:05pm UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576/6 "2012-04-14T12:05:08Z")

</div>

Juan Pablo Avello wrote in post #1056480:

> El sbado, 14 de abril de 2012 13:12:19 UTC+2, Ruby-Forum.com User escribi:
> 
> > class CourseLesson \< ActiveRecord::Base CourseDate.where(:date =\> Date.today, :canceled\_time =\> nil) to get my original query working.
> > 
> > Any hints how ?
> > 
> > Thanks
> > 
> > Dani
> > 
> > -- Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Certainly, that changes things a lot; find\_by\_\* or where() constructed like this, expect all attributes used to be on the same table. If you need data from another table, you must join those tables in sql, so you need to add includes() or join() to your method chain.
> 
> Using 'where', your query should look like:
> 
> CourseDate.includes(:course\_lessons).where(:date =\> Date.today, :course\_lessons =\> { :cancelled\_time =\> nil})
> 
> or:
> 
> CourseDate.includes(:course\_lessons).where("course\_dates.date = ? and course\_lessons.cancelled\_time is null", Date.today)
> 
> Regards.

Hi Juan, Thank you, both suggested queries are working fine !

But still I don't understand why my query is not working, I'm getting all lessons, so also without using include, through the association, rails knows to build the correct sql query, the only thing not working is, that also lessons with timestamp are selected. Why using 'nil' ignores the fact that timestamp is not 'nil' (as I'm expecting to select only those with 'cancelled\_time' set to 'nil') ?

regards

Dani

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Avello](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Juan\_Pablo\_Avello](https://discuss.rubyonrails.org/u/Juan_Pablo_Avello)\
**Post date:** [April 14, 2012, 12:17pm UTC](https://discuss.rubyonrails.org/t/empty-field-check/63576/7 "2012-04-14T12:17:20Z")

</div>

The main reason because your query doesn’t work is because you have the queried data in different tables and you must do the join. You should check your log and see the generated SQL queries with both options, as Colin stated, and maybe play a bit with them, check their results on your db console and so on, so you can see the differences.

Anyway, if your ‘.find\_all\_by\_date\_and\_cancelled\_time’ method works instead of throwing an exception, it should mean that you actually have a ‘cancelled\_time’ column in your ‘course\_dates’ table; maybe its there from previous tryouts or before adding ‘CourseLessons’ model.
