Jon Leighton
2006-Jan-02 12:24 UTC
[Rails] Altering the accessor of an association collection
I want to be able to do something like this (for example): class Person < ActiveRecord::Base has_many :friends def friends(living_in = :uk) # filter based on parameters here end end Is there any way I can redefine the accessor for an association whilst still being able to get the values? Basically, I''m looking for an equivilent of read_attribute() for associations. -- Posted via http://www.ruby-forum.com/.
Jon Leighton
2006-Jan-02 12:45 UTC
[Rails] Re: Altering the accessor of an association collection
Actually don''t worry about this, I changed my code so I don''t need it. -- Posted via http://www.ruby-forum.com/.
Jarkko Laine
2006-Jan-02 12:45 UTC
[Rails] Altering the accessor of an association collection
On 2.1.2006, at 14.24, Jon Leighton wrote:> I want to be able to do something like this (for example): > > class Person < ActiveRecord::Base > has_many :friends > > def friends(living_in = :uk) > # filter based on parameters here > endYou can use the automatic Active Record methods for this, too: jake = Person.find :first jake.friends.find_all_by_living_in("uk") If you think it''s worth it, you can also write your own friends method. I can''t really see a reason for that, though. Maybe I misunderstood something. //jarkko> end > > Is there any way I can redefine the accessor for an association whilst > still being able to get the values? Basically, I''m looking for an > equivilent of read_attribute() for associations. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2363 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060102/acfe2f35/smime.bin
Jon Leighton
2006-Jan-02 16:43 UTC
[Rails] Re: Altering the accessor of an association collection
Jarkko Laine wrote:> On 2.1.2006, at 14.24, Jon Leighton wrote: > >> I want to be able to do something like this (for example): >> >> class Person < ActiveRecord::Base >> has_many :friends >> >> def friends(living_in = :uk) >> # filter based on parameters here >> end > > You can use the automatic Active Record methods for this, too: > > jake = Person.find :first > jake.friends.find_all_by_living_in("uk") > > If you think it''s worth it, you can also write your own friends > method. I can''t really see a reason for that, though. Maybe I > misunderstood something.Perhaps my example wasn''t the best -- it''s actually just a simplified example of something I''m doing which isn''t anything to do with People or Friends. Basically I have a recursive object which makes up a tree of nodes -- imagine friends being of class Person as well (so I guess it would have been better to say "has_many :people" or something). Sometimes I would want friends and its all descendants; sometimes I just want friends; and sometimes I want friends, and descendants, excluding where a boolean attribute is true. Anyway, thanks for the info; I hadn''t thought of calling finders on a collection like that, but I''ve found an adaquate way to solve the problem. Cheers -- Posted via http://www.ruby-forum.com/.