I''ve recently implemented an "active" flag on several of my
models in
a very large codebase, and I''d rather not have to go back and change
every Model.find() instance to specify the new conditions - {:active
=> true}
So I was thinking: how can I make the model itself automatically
append that to the conditions passed in?
I tried this, but to no avail:
(in app/models/model.rb)
Class Something < ActiveRecord::Base
# ...
  # Overwriting the find method to exclude anything NOT active.
  def find
    if @conditions.blank?
      @conditions = "active = true"
    else
      @conditions = @conditions + " AND active = true"
    end
    super
  end
# ...
end
Unfortunately, my tests didn''t work.  I went in and set the first row
in my somethings table to be inactive, but find(:first) still returned
it.
I don''t see an activerecord call back like "before_find" or
something
(though I''ve read about, but can''t seem to find documented, an
"after_find").
Does anyone know how I can do this, or is manually editing every
single Model.find instance the only way?
2009/6/12 Phoenix Rising <PolarisRising-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > I''ve recently implemented an "active" flag on several of my models in > a very large codebase, and I''d rather not have to go back and change > every Model.find() instance to specify the new conditions - {:active > => true} > > So I was thinking: how can I make the model itself automatically > append that to the conditions passed in? >I think default_scope will do what you want here. For each model: default_scope :conditions => {:active => true} I am not sure at which version of rails default_scope was introduced. Colin
Marnen Laibow-Koser
2009-Jun-12  23:44 UTC
Re: Find conditions on the model without a new method
Colin Law wrote: [...]> I am not sure at which version of rails default_scope was introduced.2.3, I believe.> > ColinBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.