Hello, I was wondering if it''s possible to add :conditions to an association, such that the conditions reference variables that need to be re- evaluated before each query? e.g.: has_many :people, :conditions => "some_column @some_instance_variable, revision_time > #{Time.now}," etc. When I tried an example binding a date column to Time.now, the time didn''t change with each query. Instead, it continued using the initial value of Time.now from when the class loaded. Thanks, Tom --~--~---------~--~----~------------~-------~--~----~ 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
2007-Nov-20 04:06 UTC
Re: association :conditions & instance-variable evaluation
On 20 Nov 2007, at 00:33, tom_302 wrote:> > Hello, > > I was wondering if it''s possible to add :conditions to an association, > such that the conditions reference variables that need to be re- > evaluated before each query? > > e.g.: > has_many :people, :conditions => "some_column > @some_instance_variable, revision_time > #{Time.now}," etc. > > When I tried an example binding a date column to Time.now, the time > didn''t change with each query. Instead, it continued using the > initial value of Time.now from when the class loaded.You can interpolate these things, but as you found has_many :foos, :conditions => "#{xyz}" doesn''t work: this statement is ran at class load and so the string is interpolated has_many :foos, :conditions => ''#{xyz}'' should do the trick. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jason Perry
2007-Nov-27 15:28 UTC
Re: association :conditions & instance-variable evaluation
> has_many :foos, :conditions => ''#{xyz}''Here'' my problem with the interpolation: :conditions => ''allow_spatial_resampling = #{instance_var_that_evals_to_true}'' # => WHERE (allow_spatial_resampling = true) Great, that works for MySQL, but not, say, SQLite. I need to do: :conditions => [''allow_spatial_resampling = ?'', instance_var_that_evals_to_true] # => MySQL: WHERE (allow_spatial_resampling = 1) # => SQLite: WHERE (allow_spatial_resampling = ''t'') How can I have a DB agnostic solution, where obviously the latter won''t work at the class level. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Jason Perry
2007-Nov-27 15:51 UTC
Re: association :conditions & instance-variable evaluation
> How can I have a DB agnostic solution, where obviously the latter won''t > work at the class level.At least for my example, I found the solution: :conditions => ''allow_spatial_resampling = #{quote_value(instance_var)}'' That works as expected, hope this helps someone out there. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---