有没有办法使用AREL进行自定义关联?

时间:2011-04-25 22:13:17

标签: ruby-on-rails ruby activerecord associations arel

model Post
  # ActiveRecord associations have tons of options that let
  # you do just about anything like:
  has_many :comments
  has_many :spam_comments, :conditions => ['spammy = ?', true]

  # In Rails 3, named scopes are ultra-elegant, and let you do things like:
  scope :with_comments, joins(:comments)
end

有没有办法使用AREL或其他更精简的语法来定义自定义关联,就像命名范围一样优雅?

更新

我认为将这种细节放入一个关联中并不是一个好主意,因为关联应该总是/主要定义模型之间的基本关系。

2 个答案:

答案 0 :(得分:3)

其中一个解决方案是将垃圾内容放在评论上:

model Post
  has_many :comments

  scope :with_comments, joins(:comments)
end

model Comment
  scope :spammy, where(:spammy => true)
end

对于模型职责而言,这看起来更清晰一些。性能方面完全相同:

p.comments.spammy.to_sql
# → SELECT "comments".* FROM "comments"
#   WHERE ("comments".post_id = 2) AND ("comments"."spammy" = "t")

额外的好处:您可以从任何其他协会获得垃圾评论。

答案 1 :(得分:2)

嗯,可能有更好的方法,但我知道您可以使用Arel的Arel功能在关联中使用实际的ActiveRecord::Relation条件(而不是to_sql s)。< / p>

has_many :spam_comments, :class_name => 'Comment', :conditions => Comment.arel_table[:spammy].eq(true).to_sql

您会注意到实际的Arel代码并不像ActiveRecord Relations那样精简。

我确实在Rails主分支中找到了a comment,它引用了将Arel谓词作为条件传递,但该代码似乎不在3.0分支中。至少不是我能找到的。

相关问题