Let''s say I have a table "foo_and_bar" and I want to create a
class
"Foo" that only contains the foo records.  I''d like to do
something like:
  class Foo < ActiveRecord::Base
    set_table_name "foo_and_bar"
    constrain :conditions => "type = ''bar''"
  end
But "constrain" only works for a given block.  How do I make a class 
*always* restrict itself to a subset of the rows in a table?
David Felstead
2005-Aug-14  23:23 UTC
Re: "constrains" -type conditions for ActiveRecord class
Off hand I don''t think there''s a way to do this directly in
Rails...
however, the functionality you''re describing is pretty much that of a
view...  Perhaps you could wrap your table in a view?  This is fine if
you don''t need to update it - though you can have updatable views in
PostgreSQL and Oracle IIRC.
Alternatively, you could override ActiveRecord::Base#find - something
like this (untested!!):
class Foo < ActiveRecord::Base
  class << self # Used to override/add class methods
    alias_method :find_without_constraints, :find
    alias_method :find, :find_with_constraints
    
    def find_with_constraints(*args)
      find_without_constraints(args, conditions => ["type =
''bar''"])
    end
  end
end
Totally untested, but I think that''s the jist of it.  Actually, come
to think of it, you''d probably have to merge the *args list with the
conditions => ["type = ''bar''"] in the code above,
but that''s pretty
straightforward.  If you wanted to, you could wrap the code above in a
Module to add this kind of ''view'' functionality to Rails
(complete
with a built in validator to auto-assign new model object with the
constraint column values :).
Hope that helps!
-DF
On 8/15/05, Steve Downey
<sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org>
wrote:> Let''s say I have a table "foo_and_bar" and I want to
create a class
> "Foo" that only contains the foo records.  I''d like to
do something like:
> 
>   class Foo < ActiveRecord::Base
>     set_table_name "foo_and_bar"
>     constrain :conditions => "type = ''bar''"
>   end
> 
> But "constrain" only works for a given block.  How do I make a
class
> *always* restrict itself to a subset of the rows in a table?
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
David Felstead
2005-Aug-14  23:42 UTC
Re: "constrains" -type conditions for ActiveRecord class
Oh, and sorry to reply to myself - you''ll have to override find_all as well. Cheers! -DF On 8/15/05, David Felstead <david.felstead-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Off hand I don''t think there''s a way to do this directly in Rails... > however, the functionality you''re describing is pretty much that of a > view... Perhaps you could wrap your table in a view? This is fine if > you don''t need to update it - though you can have updatable views in > PostgreSQL and Oracle IIRC. > > Alternatively, you could override ActiveRecord::Base#find - something > like this (untested!!): > > class Foo < ActiveRecord::Base > class << self # Used to override/add class methods > alias_method :find_without_constraints, :find > alias_method :find, :find_with_constraints > > def find_with_constraints(*args) > find_without_constraints(args, conditions => ["type = ''bar''"]) > end > end > end > > Totally untested, but I think that''s the jist of it. Actually, come > to think of it, you''d probably have to merge the *args list with the > conditions => ["type = ''bar''"] in the code above, but that''s pretty > straightforward. If you wanted to, you could wrap the code above in a > Module to add this kind of ''view'' functionality to Rails (complete > with a built in validator to auto-assign new model object with the > constraint column values :). > > Hope that helps! > > -DF > > On 8/15/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote: > > Let''s say I have a table "foo_and_bar" and I want to create a class > > "Foo" that only contains the foo records. I''d like to do something like: > > > > class Foo < ActiveRecord::Base > > set_table_name "foo_and_bar" > > constrain :conditions => "type = ''bar''" > > end > > > > But "constrain" only works for a given block. How do I make a class > > *always* restrict itself to a subset of the rows in a table? > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Joshua Sierles
2005-Aug-15  01:29 UTC
Re: "constrains" -type conditions for ActiveRecord class
You may want to consider the buit-in Rails STI functionality, whereby you can use that type field to define a class as a subclass of the model that associates with the table. For example: user.rb class User < ActiveRecord::Base class Person < User This work automatically as long as your ''type'' field is named ''type'' and uses camel case class names as values (Foo, Bar). Joshua Sierles On 8/14/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:> > Let''s say I have a table "foo_and_bar" and I want to create a class > "Foo" that only contains the foo records. I''d like to do something like: > > class Foo < ActiveRecord::Base > set_table_name "foo_and_bar" > constrain :conditions => "type = ''bar''" > end > > But "constrain" only works for a given block. How do I make a class > *always* restrict itself to a subset of the rows in a table? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails