【问题标题】:Converting Rails 4 has_many转换 Rails 4 has_many
【发布时间】:2014-07-10 21:43:51
【问题描述】:

如何将带有 :conditions 的旧 Rails has_many 转换为新的 -> 语法?

这里是has_many:

  has_many assets_name.to_sym, through: :associated_assets,
    conditions: { assets: { type: asset_subclass_name } },
    source: asset_subclass_name.underscore

【问题讨论】:

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


    【解决方案1】:

    过程是将条件块转换为 -> {} 内的有效 rails 4 条件。条件需要是 has_many 的第二个参数。

    has_many assets_name.to_sym, -> { where(type: asset_subclass_name)},
      through: :associated_assets,
      source: asset_subclass_name.underscore
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 2015-05-30
      • 1970-01-01
      相关资源
      最近更新 更多