El sábado, 14 de abril de 2012 13:12:19 UTC+2, Ruby-Forum.com User escribió:
>
> 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
>
> --
> 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.
--
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/-/vj8BevpU4nYJ.
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.