【问题标题】:Rails 4 model associations conditionsRails 4 模型关联条件
【发布时间】:2013-12-26 09:33:27
【问题描述】:

我有一个与条件具有多态关联的模型。

但这似乎不起作用,听起来 conditions 在 Rails 4 中已被弃用。

以下是我的关联代码:

has_one :top_background, :class_name => 'UploadedFile', as: :imageable,
            :conditions => { :imageable_type => 'venue_top_background' }, dependent: :destroy

有没有办法在 Rails 4 中完成这项工作?

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

在 Rails 4 中,条件是通过一个作用域块作为 has_one 的第二个参数来实现的。例如:

has_one :top_background, ->{where(imageable_type: 'venue_top_background')}, :class_name => 'UploadedFile', as: :imageable, dependent: :destroy

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多