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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.