ruby-on-rails – Has_Many:通过或:finder_sql

我已经确定了我想要的东西,但是我似乎无法按照钢轨设计师的要求来找到它.基本上,我有(请搁置复数/等问题):

人的
关系(父母,子孙)

我试图让一个父母的所有后代,以及许多后代的单亲(假定每个后代只有一个父母).

我可以通过以下方式在模型中做到这一点:

has_one     :parent,:through => :relationships,:foreign_key => :human_id,:source => :source_human
has_many    :offsprings,:finder_sql =>
          'SELECT disTINCT offsprings.* ' +
          'FROM humans offsprings INNER JOIN relationships r on ' +
          'r.human_id = offsprings.id where r.source_human_id = #{id}'

我不得不这样做,因为更好的做法:

has_many    :offsprings,:foreign_key => :source_human_id,:source => :human

是不可能的,因为外键在has_many中被忽略(根据这里的文档:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many)

但是,现在我得到这个错误

DEPRECATION WARNING: String-based
interpolation of association
conditions is deprecated. Please use a
proc instead. So,for example,
has_many :older_friends,:conditions
=> ‘age > #{age}’ should be changed to has_many :older_friends,:conditions
=> proc { “age > #{age}” }. (called from irb_binding at (irb):1)

然而,无论我如何闯入:条件在这里,似乎没有:finder_sql想参加.有什么想法吗?

解决方法

如果你这样做
has_many    :offsprings,:finder_sql =>
          proc { "SELECT disTINCT offsprings.* " +
          "FROM humans offsprings INNER JOIN relationships r on " +
          "r.human_id = offsprings.id where r.source_human_id = #{id}" }

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...