MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-Nov-12 19:32 UTC
dynamic condition for has_one and eager loading issue
Hi, I ve defined the following relation in one of my models with a dynamic where condition: has_one :selection, :foreign_key => ''object_id'', :conditions => ''selection_type = 1 and account_id = # {self.send(:account_id)}'' That works perfect, however when I try to eager load that relation I am getting the following error when doing a count. Seems that self is not really my model class anymore.... Does anybody know how I can get that up and running? Thanks a lot in advance NoMethodError (undefined method `account_id'' for #<ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation: 0x4d5bf5c>): C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations.rb:1621:in `interpolate_sql'' (eval):1:in `interpolate_sql'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations.rb:1600:in `association_join'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations.rb:1599:in `each'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/associations.rb:1599:in `association_join'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:184:in `construct_calculation_sql'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:184:in `collect'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:184:in `construct_calculation_sql'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:212:in `execute_simple_calculation'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:121:in `calculate'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:117:in `catch'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:117:in `calculate'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/calculations.rb:45:in `count'' --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-12 22:06 UTC
Re: dynamic condition for has_one and eager loading issue
On Nov 12, 7:32 pm, "MrBana...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <MrBana...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hi, > > I ve defined the following relation in one of my models with a dynamic > where condition: > has_one :selection, > :foreign_key => ''object_id'', > :conditions => ''selection_type = 1 and account_id = # > {self.send(:account_id)}'' > > That works perfect, however when I try to eager load that relation I > am getting the following error when doing a count. Seems that self is > not really my model class anymore.... Does anybody know how I can get > that up and running? >When you do a ''normal'' load the conditions are interpolated in the context of the appropriate instance, but when you do an eager load that can''t happen (because the object is not loaded yet (so account_id is not known) or because you are loading multiple record) so the conditions are evaluated in the context of the class. You just can''t eager load stuff with that sort of interpolated condition, although I suppose you might be able to put together a separate association, purely for eager loading, where the conditions would become account_id = parent_objects.account_id. Fred> Thanks a lot in advance > > NoMethodError (undefined method `account_id'' for > #<ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation : > 0x4d5bf5c>): > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/associations.rb:1621:in `interpolate_sql'' > (eval):1:in `interpolate_sql'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/associations.rb:1600:in `association_join'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/associations.rb:1599:in `each'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/associations.rb:1599:in `association_join'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:184:in `construct_calculation_sql'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:184:in `collect'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:184:in `construct_calculation_sql'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:212:in `execute_simple_calculation'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:121:in `calculate'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:117:in `catch'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:117:in `calculate'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/calculations.rb:45:in `count''--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Nov-12 23:12 UTC
Re: dynamic condition for has_one and eager loading issue
MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Hi, > > I ve defined the following relation in one of my models with a dynamic > where condition: > has_one :selection, > :foreign_key => ''object_id'', > :conditions => ''selection_type = 1 and account_id = # > {self.send(:account_id)}'' > > That works perfect, however when I try to eager load that relation I > am getting the following error when doing a count. Seems that self is > not really my model class anymore.... Does anybody know how I can get > that up and running?http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7f7f715d917b9faa/426c768dd282929f But be careful if and when you use Rails in multi-threaded mode. -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-Nov-14 14:09 UTC
Re: dynamic condition for has_one and eager loading issue
What might happen in multithreaded mode?? -- Volker On 13 Nov., 00:12, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> MrBana...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote: > > Hi, > > > I ve defined the following relation in one of my models with a dynamic > > where condition: > > has_one :selection, > > :foreign_key => ''object_id'', > > :conditions => ''selection_type = 1 and account_id = # > > {self.send(:account_id)}'' > > > That works perfect, however when I try to eager load that relation I > > am getting the following error when doing a count. Seems that self is > > not really my model class anymore.... Does anybody know how I can get > > that up and running? > > http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/... > > But be careful if and when you use Rails in multi-threaded mode. > > -- > Rails Wheels - Find Plugins, List & Sell Plugins -http://railswheels.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Nov-15 01:20 UTC
Re: dynamic condition for has_one and eager loading issue
MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> What might happen in multithreaded mode??Any dynamic change to the conditions of an association would be common to all Rails handlers associated with that process. Prior to Rails 2.2, this is was always one, so there was no problem. But if you use Rails in multi-threaded mode, one thread could change the conditions used by another thread before those conditions are built into a DB query. So you''d either have to put an exclusive lock around that query (which may have a significant impact on the database parallelism), or rewrite the AR code to fork a thread-private condition whenever one is dynamically set. -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-Nov-18 08:36 UTC
Re: dynamic condition for has_one and eager loading issue
Wow,.... that sounds complicated. :-) However, I m really interested in this topic. Maybe you can explain me the proposed "rails way" to accomplish for example a relation which depends on the currently logged in user (just as an example).... Thanks a lot in advance... Volker --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---