Hi, is there a way to make Active Record add a condition to all queries it issues. I''d like to have AR add something like "and group_id=#{@session["group_id"]}" to all queries used to retrieve objects from the database. Thanks. Raph
Raphael Bauduin <rblists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> is there a way to make Active Record add a condition to all queries it issues. > I''d like to have AR add something like "and > group_id=#{@session["group_id"]}" to all queries used to retrieve > objects from the database.I get your point, but in this instance remember that the models based on ActiveRecord don''t know what the session is. The @session is a member of the controller objects. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
* Doug Alcorn (doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org) [050410 15:55]:> > is there a way to make Active Record add a condition to all queries it issues. > > I''d like to have AR add something like "and > > group_id=#{@session["group_id"]}" to all queries used to retrieve > > objects from the database. > > I get your point, but in this instance remember that the models based > on ActiveRecord don''t know what the session is. The @session is a > member of the controller objects.One could pass the information from the session through to the model via an argument to a model method. What if the original poster had instead phrased the question as follows? Is there a way to make Active Record add a condition to all queries it issues. I''d like to have AR add something like "and group_id=#{@group_id}" to all queries used to retrieve objects from the database. Rick -- http://www.rickbradley.com MUPRN: 988 | I used to random email haiku | think Julian was a | kind of cool guy, too.
On Apr 11, 2005 9:30 AM, Rick Bradley <rick-xSCPAUIMY+WN9aS15agKxg@public.gmane.org> wrote:> * Doug Alcorn (doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org) [050410 15:55]: > > > is there a way to make Active Record add a condition to all queries it issues. > > > I''d like to have AR add something like "and > > > group_id=#{@session["group_id"]}" to all queries used to retrieve > > > objects from the database. > > > > I get your point, but in this instance remember that the models based > > on ActiveRecord don''t know what the session is. The @session is a > > member of the controller objects. > > One could pass the information from the session through to the model via > an argument to a model method. > > What if the original poster had instead phrased the question as follows? > > Is there a way to make Active Record add a condition to all queries it > issues. I''d like to have AR add something like "and > group_id=#{@group_id}" to all queries used to retrieve objects from > the database.The best way to do this is ensure that this concept of "Groups" is part of your model. Assume you have a @session["group"] and a Group has_many :thingies Instead of having your actions execute: Thingy.find(@params["id"]) # select * from thingies where id = ? You instead navigate the associations @session["group"].thingies.find(@params["id"]) # select * from thingies where id =? and group_id = ? Which is exactly what the original poster is after.> Rick > -- > http://www.rickbradley.com MUPRN: 988 > | I used to > random email haiku | think Julian was a > | kind of cool guy, too. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On Apr 11, 2005 12:01 AM, Michael Koziarski <koziarski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 11, 2005 9:30 AM, Rick Bradley <rick-xSCPAUIMY+WN9aS15agKxg@public.gmane.org> wrote: > > * Doug Alcorn (doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org) [050410 15:55]: > > > > is there a way to make Active Record add a condition to all queries it issues. > > > > I''d like to have AR add something like "and > > > > group_id=#{@session["group_id"]}" to all queries used to retrieve > > > > objects from the database. > > > > > > I get your point, but in this instance remember that the models based > > > on ActiveRecord don''t know what the session is. The @session is a > > > member of the controller objects. > > > > One could pass the information from the session through to the model via > > an argument to a model method. > > > > What if the original poster had instead phrased the question as follows? > > > > Is there a way to make Active Record add a condition to all queries it > > issues. I''d like to have AR add something like "and > > group_id=#{@group_id}" to all queries used to retrieve objects from > > the database. > > The best way to do this is ensure that this concept of "Groups" is > part of your model. > > Assume you have a @session["group"] and a Group has_many :thingies > > Instead of having your actions execute: > > Thingy.find(@params["id"]) # select * from thingies where id = ? > > You instead navigate the associations > > @session["group"].thingies.find(@params["id"]) # select * from > thingies where id =? and group_id = ? > > Which is exactly what the original poster is after. >But that doesn''t work if you are not searching with the id. (http://ar.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000012) You can''t do @session["group"].thingies.find_all("last_update_time>''2005-04-11''") which is what I''m looking for. Thanks Raph> > > Rick > > -- > > http://www.rickbradley.com MUPRN: 988 > > | I used to > > random email haiku | think Julian was a > > | kind of cool guy, too. > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > Cheers > > Koz > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 11.4.2005, at 17:34, Raphael Bauduin wrote:> But that doesn''t work if you are not searching with the id. > (http://ar.rubyonrails.com/classes/ActiveRecord/Associations/ > ClassMethods.html#M000012) > You can''t do > > @session["group"].thingies.find_all("last_update_time>''2005-04-11''")Actually, you can. It''s documented in has_many (http://ar.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html#M000009). I''m not quite sure if it works with habtm, tho. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails