I''d like to retrieve Articles which are published. Obviously in order to do this, I write: Article.find(:all, :conditions => ''publish = 1'') What I''m wondering is, do I have a way of adding something to the Article model which makes this the default behaviour? A default condition? Example: class Article < ActiveRecord::Base :conditions => ''publish = 1'' end class ArticleController < ApplicationController def index @published_articles = Article.find(:all) end end And even better, could there be a way of overriding this default condition? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Colin, Not sure of a way to do this, but I''d just make a class method called Article#find_published or use a dynamic finder( i.e. Article.find_all_by_published(1) ) to simplify the query. I''d worry that if Article.find_all only retrieved published articles, someone else reading the code could be stuck for hours trying to figure out why unpublished articles aren''t being retrieved. - Derek On 6/22/05, Colin Ramsay <colinramsay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d like to retrieve Articles which are published. Obviously in order to do > this, I write: > > Article.find(:all, :conditions => ''publish = 1'') > > What I''m wondering is, do I have a way of adding something to the Article > model which makes this the default behaviour? A default condition? Example: > > class Article < ActiveRecord::Base > :conditions => ''publish = 1'' > end > > class ArticleController < ApplicationController > def index > @published_articles = Article.find(:all) > end > end > > And even better, could there be a way of overriding this default condition? > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Derek Haynes HighGroove Studios - http://www.highgroove.com Keeping it Simple. 404.593.4879
On 6/22/05, Colin Ramsay <colinramsay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d like to retrieve Articles which are published. Obviously in order to do > this, I write: > > Article.find(:all, :conditions => ''publish = 1'') > > What I''m wondering is, do I have a way of adding something to the Article > model which makes this the default behaviour? A default condition? Example: > > class Article < ActiveRecord::Base > :conditions => ''publish = 1'' > end > > class ArticleController < ApplicationController > def index > @published_articles = Article.find(:all) > end > end > > And even better, could there be a way of overriding this default condition? >The easiest solution is to add a static method to your Article model. class Article < ActiveRecord::Base def self.published find(:all, :conditions=>"published = 1") end end Then in your controllers you just have to call: class ArticleController < ApplicationController def index @published_articles = Article.published end end Nice, easy and no need for magic. In reality almost all uses of find should be encapsulated like this. -- Cheers Koz