Ruby on rails 范围关联模型按;创建于;
我有三种模式:用户、产品和所有权。所有权具有Ruby on rails 范围关联模型按;创建于;,ruby-on-rails,scope,model-associations,Ruby On Rails,Scope,Model Associations,我有三种模式:用户、产品和所有权。所有权具有产品标识:整数和用户标识:整数。我想通过在DESC创建的来确定我的产品范围 app/models/product.rb 。 . . 默认范围->{order('products.created_at DESC')} . . . 但是当我做user.owned\u products时,它不像在DESC创建的那样被订购。我该怎么做?我必须在我的用户模型中添加作用域吗 以下是我的用户和产品之间的关系: app/models/user.rb 。 . . 拥有
产品标识:整数
和用户标识:整数
。我想通过在DESC创建的来确定我的产品范围
app/models/product.rb
。
.
.
默认范围->{order('products.created_at DESC')}
.
.
.
但是当我做user.owned\u products
时,它不像在DESC创建的那样被订购。我该怎么做?我必须在我的用户模型中添加作用域吗
以下是我的用户和产品之间的关系:
app/models/user.rb
。
.
.
拥有很多:所有权
拥有许多:拥有产品,通过拥有权,
来源:产品
.
.
.
要设置用户自有产品的订单,可以在关联中指定。例如:
has_many :owned_products, through: :ownerships,
source: :product,
order: "created_at DESC"
谢谢你,这很有效!但是这种方法是不推荐的,我们必须使用类似于has\u many:owners\u products,->{order“created\u at DESC”},通过::ownerships,source::product
。