Hello.
I''m facing the problem where I have something like this:
class Person
has_many :things,
:conditions => "dynamic conditions depending of the
object"
end
So, the problem is that i would like to have a dynamic condition which
depends upon the object. In my particular case, it is something like
this:
has_many :things,
:conditions => {:country_code =>
''country_code_string''}
So, let''s say that the country_code_string might be
''UK'', ''US'' and so
on. How can I make this kind of association? I tried lambda, like i
could use in before_add, after_add and other callbacks, but then it
seems that Proc object''s string representation is used in WHERE
clause. Is there even possible to have such thing or should i use
something like:
has_many :thingsUS,
:conditions => {:country_code => ''US''}
has_many :thingsUK,
:conditions => {:country_code => ''UK''}
...
It doesn''t feel much of an elegant solution to me. Better ideas how to
solve this problem?
Jarmo
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 8, 10:05 am, Jarmo Pertman <jarm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello. > > I''m facing the problem where I have something like this: > class Person > has_many :things, > :conditions => "dynamic conditions depending of the object" > end > > So, the problem is that i would like to have a dynamic condition which > depends upon the object. In my particular case, it is something like > this: > has_many :things, > :conditions => {:country_code => ''country_code_string''} > > So, let''s say that the country_code_string might be ''UK'', ''US'' and so > on. How can I make this kind of association?On Dec 8, 10:05 am, Jarmo Pertman <jarm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello. > > I''m facing the problem where I have something like this: > class Person > has_many :things, > :conditions => "dynamic conditions depending of the object" > end > > So, the problem is that i would like to have a dynamic condition which > depends upon the object. In my particular case, it is something like > this: > has_many :things, > :conditions => {:country_code => ''country_code_string''} >I haven''t tried this, but you may want to give it a shot. You could set up a method on Person that scopes the association: class Person has_many :things def things_for_country(code) things.scoped(:conditions => { :country_code => code }) end end And then calling it would look like (assume @person is a Person object): @person.things_for_country(''US'') This will certainly work for *finding* records; I''m not 100% sure what will happen with building/adding objects - it is likely to work, but you should definitely write some tests... Hope this helps! --Matt Jones -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Jarmo Pertman
2009-Dec-11 12:26 UTC
Re: dynamic :conditions for activerecord associations?
Hello. Thank you for your suggestion. I tried it and it worked for finding, but adding doesn''t work unfortunately. Jarmo On Dec 9, 5:06 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 8, 10:05 am, Jarmo Pertman <jarm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello. > > > I''m facing the problem where I have something like this: > > class Person > > has_many :things, > > :conditions => "dynamic conditions depending of the object" > > end > > > So, the problem is that i would like to have a dynamic condition which > > depends upon the object. In my particular case, it is something like > > this: > > has_many :things, > > :conditions => {:country_code => ''country_code_string''} > > > So, let''s say that the country_code_string might be ''UK'', ''US'' and so > > on. How can I make this kind of association? > > On Dec 8, 10:05 am, Jarmo Pertman <jarm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello. > > > I''m facing the problem where I have something like this: > > class Person > > has_many :things, > > :conditions => "dynamic conditions depending of the object" > > end > > > So, the problem is that i would like to have a dynamic condition which > > depends upon the object. In my particular case, it is something like > > this: > > has_many :things, > > :conditions => {:country_code => ''country_code_string''} > > I haven''t tried this, but you may want to give it a shot. You could > set up a method on Person that scopes the association: > > class Person > has_many :things > def things_for_country(code) > things.scoped(:conditions => { :country_code => code }) > end > end > > And then calling it would look like (assume @person is a Person > object): > > @person.things_for_country(''US'') > > This will certainly work for *finding* records; I''m not 100% sure what > will happen with building/adding objects - it is likely to work, but > you should definitely write some tests... > > Hope this helps! > > --Matt Jones-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
I suspected that adding with << wouldn''t work; does using the build or create method work? --Matt Jones On Dec 11, 7:26 am, Jarmo Pertman <jarm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello. > > Thank you for your suggestion. > > I tried it and it worked for finding, but adding doesn''t work > unfortunately. > > Jarmo > > On Dec 9, 5:06 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On Dec 8, 10:05 am, Jarmo Pertman <jarm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello. > > > > I''m facing the problem where I have something like this: > > > class Person > > > has_many :things, > > > :conditions => "dynamic conditions depending of the object" > > > end > > > > So, the problem is that i would like to have a dynamic condition which > > > depends upon the object. In my particular case, it is something like > > > this: > > > has_many :things, > > > :conditions => {:country_code => ''country_code_string''} > > > > So, let''s say that the country_code_string might be ''UK'', ''US'' and so > > > on. How can I make this kind of association? > > > On Dec 8, 10:05 am, Jarmo Pertman <jarm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello. > > > > I''m facing the problem where I have something like this: > > > class Person > > > has_many :things, > > > :conditions => "dynamic conditions depending of the object" > > > end > > > > So, the problem is that i would like to have a dynamic condition which > > > depends upon the object. In my particular case, it is something like > > > this: > > > has_many :things, > > > :conditions => {:country_code => ''country_code_string''} > > > I haven''t tried this, but you may want to give it a shot. You could > > set up a method on Person that scopes the association: > > > class Person > > has_many :things > > def things_for_country(code) > > things.scoped(:conditions => { :country_code => code }) > > end > > end > > > And then calling it would look like (assume @person is a Person > > object): > > > @person.things_for_country(''US'') > > > This will certainly work for *finding* records; I''m not 100% sure what > > will happen with building/adding objects - it is likely to work, but > > you should definitely write some tests... > > > Hope this helps! > > > --Matt Jones-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.