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

