Empty field check

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

El sbado, 14 de abril de 2012 01:01:04 UTC+2, Ruby-Forum.com User
escribi:

Thanks for any hint.

Dani


Posted via http://www.ruby-forum.com/.

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!

On 14 April 2012 00:01, Dani D. [email protected] wrote:

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


You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw

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
has_many :course_lessons
end

CourseLesson
class CourseLesson < ActiveRecord::Base
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

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/.

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.

Juan P. 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/.

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

El sbado, 14 de abril de 2012 14:05:08 UTC+2, Ruby-Forum.com User
escribi:

Thanks
from another table, you must join those tables in sql, so you need to
CourseDate.includes(:course_lessons).where("course_dates.date = ? and
is, that also lessons with timestamp are selected. Why using ‘nil’

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.