Note: I haven’t actually tested the above, but I’ve done similar and it
works great. Credit goes to Josh S. for showing me how to do self
referential :throughs a while back via his blog. http://blog.hasmanythrough.com/
has_many :spanked, :through => :spanks_as_spankee
has_many :spankees, :through => :spanks_as_spankee
works great. Credit goes to Josh S. for showing me how to do self
referential :throughs a while back via his blog. http://blog.hasmanythrough.com/
Whoops, got an association backwards in the alternate option above. This
is what I meant…
class User < ActiveRecord::Base
has_many :spanks_as_spankee, :foreign_key=>‘spankee_id’, :class_name
=> ‘Spank’
has_many :spanks_as_spanker, :foreign_key=>‘spanker_id’, :class_name
=> ‘Spank’
has_many :spankers, :through => :spanks_as_spankee
has_many :spankees, :through => :spanks_as_spanker
end
Josh S., or anyone else more familiar with how this technique works,
can probably provide much better answers. But…
Yes, normally it would point directly to the join model/table.
However, to do more complex stuff (like self referential has_many
:throughs), sometimes you gotta break out the magic wand and wave it
with enthusiasm.
I just guessing, but maybe the ‘_by’ suffix is getting some other
Rails magic applied to it. Try changing it to :spanked_bx, and see if
that helps. Either way, someone more familiar than I with the Rails
internals might be able to explain it.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.