I have a Post model that belongs to a single category and author. Users may create “favorites” for categories and authors. How can I most efficiently query a list of all posts, but with the visitor’s preferred categories and/or authors sorted to the top?
class Post < ActiveRecord::Base
belongs_to :category
belongs_to :author
end
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :category # favorite category
belongs_to :author # favorite author
end
class User < ActiveRecord::Base
has_many :favorites
end
Alternate: less number of queries, but user model fetches data from
FavoriteThis code is not tested, but gives the idea.