ct9a
2010-Apr-07 12:58 UTC
Recommendation for searching with regards to timestamp & foreign key attributes
hi guys, I need a recommendation for searching with regards to timestamp & foreign key attributes. Sounds a bit too much but here''s an example. Suppose we have a "blog" object. It has many attributes such as - title - content - status_id - created_at - updated_at There are also "status" objects which have the following statuses, "new", "edit","published","archived". That''s why the "blog" object has a foreign key, "status_id". Now, suppose I want to search for all blog objects that have been created in the past 24 hours. I would do new_blogs = Blog.all(:conditions => { :created_at => (Time.now - 24.hours) .. (Time.now) } ) Tested and that works just fine. Suppose I would like to get all blog entries which are not of the status of ''published'' or ''archived''. I would just make use of searchlogic in the following way: new_blog_entries = Blog.searchlogic(:status_name_does_not_equal_all => ([''published'', ''archived'']) ) OR new_blog_entries = Blog.status_name_does_not_equal_any([''published'', ''archived'']) Question: ======= Suppose I now want to get blog entries which 1) have been created within the last 24 hours 2) have a status other than published and archived. Any recommendations for doing this? I couldn''t quite work out how to specify the status (as it''s a foreign key) to the Blog model in the ":conditions" AND similarly, I could not quite work out how to specify the "created within the last 24 hours" filter using searchlogic. I tried "new_blog_entries Blog.status_name_does_not_equal_any([''published'', ''archived''])_and_created_at((Time.now - 24.hours) .. (Time.now))" but I got an ugly error of " SyntaxError: (irb):42: syntax error, unexpected tIDENTIFIER, expecting $end ...''published'', ''archived''])_and_created_at((Time.now - 24.hours) .. (Ti... ... ^ from /usr/local/bin/irb:12:in `<main>''>> "Any ideas? thanks -- 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.
ct9a
2010-Apr-07 13:00 UTC
Recommendation for searching with regards to timestamp & foreign key attributes
Sorry guys, please replace " new_blog_entries = Blog.status_name_does_not_equal_any([''published'', ''archived'']) " with " new_blog_entries = Blog.status_name_does_not_equal_all([''published'', ''archived'']) " -- 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.
Franz Strebel
2010-Apr-07 13:44 UTC
Re: Recommendation for searching with regards to timestamp & foreign key attributes
If you are using Rails 2.1 or greater, then named_scope would be very useful. In your Blog model for example, you can specify a recent named_scope to return all entries less than 24 hours old. named_scope :recent, :conditions => [ "created_at > ?", Time.now - 24.hours ] and maybe another one for unpublished entries # modify the conditions and include accordingly as I don''t know enough about # your models named_scope :unpublished, :include => :status, :conditions => "statuses.status != ''published''" In your controllers then, you can do something like @new_unpublished_entries = Blog.unpublished.recent -- 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.
Gordon Yeong
2010-Apr-07 13:48 UTC
Re: Recommendation for searching with regards to timestamp & foreign key attributes
bravo! I like the idea, Franz! Thank you :) On 7 April 2010 23:44, Franz Strebel <franz.strebel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you are using Rails 2.1 or greater, then named_scope would be very > useful. > > In your Blog model for example, you can specify a recent named_scope > to return all entries less than 24 hours old. > > named_scope :recent, :conditions => [ "created_at > ?", Time.now - 24.hours > ] > > and maybe another one for unpublished entries > > # modify the conditions and include accordingly as I don''t know enough > about > # your models > named_scope :unpublished, :include => :status, :conditions => > "statuses.status != ''published''" > > In your controllers then, you can do something like > > @new_unpublished_entries = Blog.unpublished.recent > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.