Has_Many :通过 或 :finder_sql

发布于 2024-10-27 03:37:59 字数 1278 浏览 0 评论 0原文

我已经确定了我想要的东西,但我似乎无法以 Rails 设计师正在寻找的方式得到它。基本上,我有(请抛开复数/等问题):

人类 关系(父母,后代)

我试图获取单亲的所有后代,以及许多后代的单亲(假设每个后代只有一个父母)。

我可以在模型中按以下方式执行此操作:

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, :through => :relationships, :foreign_key => :source_human_id, :source => :human

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

但是,现在我收到此错误:

弃用警告:基于字符串 关联插值 条件已被弃用。请使用 相反。所以,举例来说, has_many :older_friends, :条件 => '年龄> #{age}' 应更改为 has_many :older_friends, :conditions => proc {“年龄>#{年龄}”}。 (从 (irb):1 处的 irb_binding 调用)

但是,无论我如何破解这里的 :conditions,似乎 :finder_sql 都不想参与。有什么想法吗?

I've nailed down what I want, but I can't seem to get it in a way that the rails designers are looking for. Basically, I have (please set aside pluralization/etc issues):

Human
Relationships (Parent, Offspring)

I'm trying to get all the offsprings for a single parent, and the single parent for many offsprings (assume only one parent per offspring).

I can do this in the following way in the model:

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}' 

I had to do this, because the nicer way to do it:

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

Is not possible because foreign keys are ignored in has_many (according to the docs here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many)

However, now I'm getting this error:

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)

However, no matter how I hack at :conditions here, it does not appear that :finder_sql wants to participate. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2024-11-03 03:37:59

如果你这样做怎么办

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}" }

What if you do

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}" }
想挽留 2024-11-03 03:37:59

实际上,我会这样写:

has_many :offsprings, :finder_sql => proc {OFFSPRING_SQL % {id: id}}

OFFSPRING_SQL = "SELECT DISTINCT offsprings.*
                   FROM humans offsprings
                  INNER JOIN relationships r
                        ON r.human_id = offsprings_id
                        WHERE r.source_human_id = %{id}"

我认为这使关联更容易理解,并且使裸 SQL 更易于编辑。它还利用基于字符串的参数插值。

Actually, I'd write it this way:

has_many :offsprings, :finder_sql => proc {OFFSPRING_SQL % {id: id}}

OFFSPRING_SQL = "SELECT DISTINCT offsprings.*
                   FROM humans offsprings
                  INNER JOIN relationships r
                        ON r.human_id = offsprings_id
                        WHERE r.source_human_id = %{id}"

I think it makes the association easier to understand, and it make the naked SQL easier to edit. It also takes advantage of string-based parameter interpolation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文