dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org
2005-Oct-11 08:37 UTC
ActiveRecord relations and dynamic conditions
When using ActiveRecord ''has_many'' feature one can use '':conditions'' criteria e.g. class Contact < ActiveRecord::Base # assotiations belongs_to :company has_many :problems, :conditions => "some condition" end Is there any way how to dynamicly specify the condition in has_many statement ? Thanks for any advice, David Marko _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Don''t specify :conditions in the has_many, but instead in .find -- -- Tom Mornini On Oct 11, 2005, at 1:37 AM, dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org wrote:> > When using ActiveRecord ''has_many'' feature one can use > '':conditions'' criteria e.g. > > class Contact < ActiveRecord::Base > > # assotiations > belongs_to :company > has_many :problems, :conditions => "some condition" > end > > Is there any way how to dynamicly specify the condition in has_many > statement ? > > Thanks for any advice, > > David Marko > _______________________________________________ > 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
Julian ''Julik'' Tarkhanov
2005-Oct-11 13:06 UTC
Re: ActiveRecord relations and dynamic conditions
On 11-okt-2005, at 10:37, dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org wrote:> > When using ActiveRecord ''has_many'' feature one can use > '':conditions'' criteria e.g. > > class Contact < ActiveRecord::Base > > # assotiations > belongs_to :company > has_many :problems, :conditions => "some condition" > end > > Is there any way how to dynamicly specify the condition in has_many > statement ?I think you can use "lazy evaluation" in the form of :condition=>''some_column = {condition_method_on_model}'' -- Julian "Julik" Tarkhanov
dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org
2005-Oct-11 13:14 UTC
Re: ActiveRecord relations and dynamic conditions
Hmm, but model methods have no access to ''controller'' objects like ''session'' is. To be more specific I want to filter data based on current logged user to be sure that anly allowed data are listed. Users info is stored in session and model methods have no access to it. David Marko "Julian ''Julik'' Tarkhanov" <listbox-RY+snkucC20@public.gmane.org> Sent by: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org 11.10.2005 15:06 Please respond to rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org To rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org cc Subject Re: [Rails] ActiveRecord relations and dynamic conditions On 11-okt-2005, at 10:37, dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org wrote:> > When using ActiveRecord ''has_many'' feature one can use > '':conditions'' criteria e.g. > > class Contact < ActiveRecord::Base > > # assotiations > belongs_to :company > has_many :problems, :conditions => "some condition" > end > > Is there any way how to dynamicly specify the condition in has_many > statement ?I think you can use "lazy evaluation" in the form of :condition=>''some_column = {condition_method_on_model}'' -- Julian "Julik" Tarkhanov _______________________________________________ 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
Julian ''Julik'' Tarkhanov
2005-Oct-11 18:54 UTC
Re: ActiveRecord relations and dynamic conditions
On 11-okt-2005, at 15:14, dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org wrote:> > Hmm, but model methods have no access to ''controller'' objects like > ''session'' is. To be more specific I want to filter data based on > current logged user to be sure that anly allowed data are listed. > Users info is stored in session and model methods have no access > to it. > >Yes, this decoupling is there intentionally and personally I think it''s right. I also encountered this problem, for now I see only these solutions. 1. I implement a kind of a security filter, which can do things like if @filter.can_view?(@loaded_object) proceed... else deny end This it an object that gets loaded with the controller and is constructed passing it a User model on creation. Then in this object I quickly write "delegate" actions that deal with specific models, for example begin @filter.update_attributes(@job, params[:job]) rescue PermissionError #it is forbidden end In the filter I can define methods like "can_view_support_ticket? (ticket)" or "can_update_notice?(notice)" The object will by itself dispatch a question about permissions to this method if it gets a SupportTicket object. This is in a way similar to the recently posted Model Security tutorial - I think it''s indeed wise to delegate security management to a separate class/object instead of making your controllers checking each and ever condition. What you might use is simply a find method on the collection, like Technician.find(3).support_tickets.find("owner_id = #{@user.id}") -- Julian "Julik" Tarkhanov
It''s a bit of a hack, but adding a before_filter to your application controller like this: before_filter :assign_session <snip> def assign_session $session = session end Will allow you access to your session in your models through $session. Of course, this probably violates the principles of MVC, as you have controller code in your models. Elliot On 11/10/05, dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org <dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org> wrote:> > Hmm, but model methods have no access to ''controller'' objects like > ''session'' is. To be more specific I want to filter data based on current > logged user to be sure that anly allowed data are listed. Users info is > stored in session and model methods have no access to it. > > David Marko > > > > > "Julian ''Julik'' Tarkhanov" <listbox-RY+snkucC20@public.gmane.org> > Sent by: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > 11.10.2005 15:06 > > Please respond to > rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > To rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > cc > > Subject Re: [Rails] ActiveRecord relations and dynamic conditions > > > > > > > On 11-okt-2005, at 10:37, dmarko-KxaEA3z1jV+iEHBPiYQIQwC/G2K4zDHf@public.gmane.org wrote: > > > > > When using ActiveRecord ''has_many'' feature one can use > > '':conditions'' criteria e.g. > > > > class Contact < ActiveRecord::Base > > > > # assotiations > > belongs_to :company > > has_many :problems, :conditions => "some condition" > > end > > > > Is there any way how to dynamicly specify the condition in has_many > > statement ? > > I think you can use "lazy evaluation" in the form of > > :condition=>''some_column = {condition_method_on_model}'' > > -- > Julian "Julik" Tarkhanov > > _______________________________________________ > 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 > > >