【问题标题】:Rails application trying to set default_scope of a modelRails 应用程序尝试设置模型的 default_scope
【发布时间】:2014-04-10 06:41:40
【问题描述】:

在此应用程序中,模型 Resource_Estimations 由公司完成。我希望公司名称的 Resource_Estimations 有一个默认排序顺序。

公司模式

class Company < ActiveRecord::Base
   include ActiveModel::ForbiddenAttributesProtection
   ...
   has_many :resource_estimations, dependent: :destroy
   ...
   validates :exchange_id, :name, :full_name, presence: true

Resource_Estimation 模型

class ResourceEstimation < ActiveRecord::Base
   include ActiveModel::ForbiddenAttributesProtection
   ...
   belongs_to :company
   ...
   validates :company_id, :drill_id, :resource_type_id, :date,
      :fill_to_spill,:p10, :p50, :p90, presence: true
   ...
   default_scope  { order(:company => :asc) }

Resource_Estimation 模型中的最后一条语句(default_scope)是我想要改变的。我希望排序顺序是 company.name,而不是 company_id 的当前排序顺序。已经尝试了很多东西,但到目前为止没有运气。欢迎任何建议-谢谢皮埃尔

【问题讨论】:

    标签: ruby-on-rails model default-scope


    【解决方案1】:

    尝试先加入公司:

    default_scope  { joins(:company).order('companies.name ASC') }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      相关资源
      最近更新 更多