Hey Group, I am trying to use [what I thought should be] a simple scope for a message object scope :unread, where(:read_at => nil) where the read_at attribute is of type datetime This is causing the scope to trip a no method error when called upon. I have a helper method which contains : if message.read_at == nil "NEW" end and that works fine. however even scope :unread, where("read_at == ?", nil ) does not seem to want to work. I know I must be missing something terribly obvious about this. Any kick in the right direction is appreciated. Thanks Jesse -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 13, 4:16 pm, Jesse <draco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey Group, > > I am trying to use [what I thought should be] a simple scope for a > message object > > scope :unread, where(:read_at => nil) > > where the read_at attribute is of type datetime > > This is causing the scope to trip a no method error when called upon. > > I have a helper method which contains : > > if message.read_at == nil > "NEW" > end > > and that works fine. however even > > scope :unread, where("read_at == ?", nil ) >When comparing with nil you have to use IS NULL rather than the usual comparison operator. where(:read_at => nil) should work though. How are you using the scope? Fred> does not seem to want to work. > > I know I must be missing something terribly obvious about this. Any kick > in the right direction is appreciated. > > Thanks > > Jesse-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 9/13/11 12:39 PM, Frederick Cheung wrote:> > On Sep 13, 4:16 pm, Jesse<draco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hey Group, >> >> I am trying to use [what I thought should be] a simple scope for a >> message object >> >> scope :unread, where(:read_at => nil) >> >> where the read_at attribute is of type datetime >> >> This is causing the scope to trip a no method error when called upon. >> >> I have a helper method which contains : >> >> if message.read_at == nil >> "NEW" >> end >> >> and that works fine. however even >> >> scope :unread, where("read_at == ?", nil ) >> > When comparing with nil you have to use IS NULL rather than the usual > comparison operator. where(:read_at => nil) should work though. How > are you using the scope?I am making a call to it through current_user.received_messages.unread.length which trips a MYSQL Error |Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''== NULL)'' at line 1: SELECT `message_copies`.* FROM `message_copies` WHERE (`message_copies`.recipient_id = 1) AND (read_at == NULL)| Sorry I figured out the other issue.> > Fred > > >> does not seem to want to work. >> >> I know I must be missing something terribly obvious about this. Any kick >> in the right direction is appreciated. >> >> Thanks >> >> Jesse-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Sep 13, 5:51 pm, Jesse <draco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/13/11 12:39 PM, Frederick Cheung wrote: > > When comparing with nil you have to use IS NULL rather than the usual > > comparison operator. where(:read_at => nil) should work though. How > > are you using the scope? > > I am making a call to it through > current_user.received_messages.unread.length which trips a MYSQL Error > > |Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''== NULL)'' at line 1: SELECT `message_copies`.* FROM `message_copies` WHERE (`message_copies`.recipient_id = 1) AND (read_at == NULL)| >is that using where("read_at == ?", nil ) ? That''s just invalid syntax, since the comparison operator operator is = in SQL. NULL is a special case though - use IS NULL/IS NOT NULL to test for null values. Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 9/13/11 1:04 PM, Frederick Cheung wrote:> On Sep 13, 5:51 pm, Jesse<draco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 9/13/11 12:39 PM, Frederick Cheung wrote: >>> When comparing with nil you have to use IS NULL rather than the usual >>> comparison operator. where(:read_at => nil) should work though. How >>> are you using the scope? >> I am making a call to it through >> current_user.received_messages.unread.length which trips a MYSQL Error >> >> |Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''== NULL)'' at line 1: SELECT `message_copies`.* FROM `message_copies` WHERE (`message_copies`.recipient_id = 1) AND (read_at == NULL)| >> > is that using where("read_at == ?", nil ) ? That''s just invalid > syntax, since the comparison operator operator is = in SQL. NULL is a > special case though - use IS NULL/IS NOT NULL to test for null values.Thanks, that did the trick.> > Fred >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.