I have three models: users, products and ownership One is owned by product_id: integer and a
user_id: integer . I want to give my products the scope by
created_at DESC .
App / model / product.rb
. . Default_scope - & gt; {Order ('products.created_at DESC')}. . But when I do user.owned_products , then it has not been ordered as created_at DESC . How can I do that? Will I add an opportunity to my user model?
Here is a connection between my users and products:
App / model / user.rb
. . . Has_many: is proprietary has_many: own_products, through :: proprietorship, source :: product. .
To set an order on products owned by a user, You can specify. For example:
has_many: own_products, via :: Ownership, Source :: Products, Order: "created_at DESC"
No comments:
Post a Comment