El sábado, 14 de abril de 2012 14:05:08 UTC+2, Ruby-Forum.com User escribió:
>
> 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/.
> >>
> > 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
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

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.


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/1fmpyi8MzpsJ.
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.

Reply via email to