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

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Juan Pablo Avello at Apr 14, 2012 at 10:07 am
    El sábado, 14 de abril de 2012 01:01:04 UTC+2, Ruby-Forum.com User escribió:
    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

    --
    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!

    --
    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/-/wRQ3nMHOzzEJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Apr 14, 2012 at 10:18 am

    On 14 April 2012 00:01, Dani Dani 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
    What is wrong here ?

    Thanks for any hint.

    Dani

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


    --
    gplus.to/clanlaw

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Dani Dani at Apr 14, 2012 at 11:12 am
    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/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Juan Pablo Avello at Apr 14, 2012 at 11:40 am
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Dani Dani at Apr 14, 2012 at 12:05 pm

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Juan Pablo Avello at Apr 14, 2012 at 12:17 pm
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 13, '12 at 11:01p
activeApr 14, '12 at 12:17p
posts7
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase