按多对多关系(rails)的JOIN表中的列排序

时间:2021-04-21 20:17:15

I have a many to many relationship between category and post. The join table is category_post_relationships.

我和类别之间有很多关系。连接表是category_post_relationships。

class Post < ActiveRecord::Base
    has_many :categories, through: :category_link_relationships
    has_many :category_post_relationships , :dependent => :destroy 
end

class Category < ActiveRecord::Base
    has_many :posts, through: :category_link_relationships
    has_many :category_post_relationships , :dependent => :destroy 
end

class CategoryPostRelationship < ActiveRecord::Base
    belongs_to :post
    belongs_to :category
end

If I have a category, the category can query for all posts by category.posts. And I want to sort these posts by the created_at of the join table category_link_relationships. There can be only one record for each category and post. I want to sort the links by the column created_at of related relationship records.

如果我有一个类别,该类别可以按category.posts查询所有帖子。我想通过连接表category_link_relationships的created_at对这些帖子进行排序。每个类别和帖子只能有一条记录。我想按相关关系记录的created_at列对链接进行排序。

For example, post 1 was created and associated to category A. Then, the post 1 was associated to category B. Post 2 was then created and associated to category B. category B now has post 1 and post 2. I want to sort post 1 and post 2 by the created_at of the relationships, not the created_at of posts.

例如,创建了帖子1并将其与类别A相关联。然后,帖子1与类别B相关联。然后创建帖子2并将其与类别B相关联。类别B现在具有帖子1和帖子2.我想对帖子进行排序1和帖子2由关系的created_at,而不是posts_at的帖子。

Thanks.

谢谢。

2 个解决方案

#1


0  

class Post < ActiveRecord::Base
    has_many :category_link_relationships , :dependent => :destroy
    has_many :categories, through: :category_link_relationships
end

class Category < ActiveRecord::Base
    has_many :category_link_relationships , :dependent => :destroy
    has_many :posts, through: :category_link_relationships
end

and now you can find posts in next way:

现在你可以通过下一个方式找到帖子:

@category.posts.joins(:category_link_relationships).order('category_link_relati‌​onships.created_at')

#2


0  

If you will always order this way, you can use a default scope on the join table.

如果您始终以这种方式订购,则可以在连接表上使用默认范围。

class CategoryLinkRelationship < ApplicationRecord
  belongs_to :post
  belongs_to :category

  default_scope { order(created_at: :asc)}
end

This will be automatically picked up by ActiveRecord. Be careful, if there is also a default scope on Category it will override the sort.

这将由ActiveRecord自动获取。注意,如果Category上也有默认范围,它将覆盖排序。

#1


0  

class Post < ActiveRecord::Base
    has_many :category_link_relationships , :dependent => :destroy
    has_many :categories, through: :category_link_relationships
end

class Category < ActiveRecord::Base
    has_many :category_link_relationships , :dependent => :destroy
    has_many :posts, through: :category_link_relationships
end

and now you can find posts in next way:

现在你可以通过下一个方式找到帖子:

@category.posts.joins(:category_link_relationships).order('category_link_relati‌​onships.created_at')

#2


0  

If you will always order this way, you can use a default scope on the join table.

如果您始终以这种方式订购,则可以在连接表上使用默认范围。

class CategoryLinkRelationship < ApplicationRecord
  belongs_to :post
  belongs_to :category

  default_scope { order(created_at: :asc)}
end

This will be automatically picked up by ActiveRecord. Be careful, if there is also a default scope on Category it will override the sort.

这将由ActiveRecord自动获取。注意,如果Category上也有默认范围,它将覆盖排序。