class Cafe < ActiveRecord::Base
belongs_to :user
has_many :posts, dependent: :destroy
has_many :tags, dependent: :destroy
end
This is cafe.rb model
class Post < ActiveRecord::Base
belongs_to :cafe
belongs_to :user
end
and this is postq.rb. the message is similar to the answers.
I want to create a new cafe array and sort it by cafe.posts.count. Finally, I will show you two of the best cafes for which there are many answers.
I tried some sorting codes, for example
@cafe_by_posts = Cafe.joins(:post).order('posts.size dsc')
but it didn’t work. The error says that the cafe has no connection with the mail. how can i sort a ruby array using a matched number of models?
I am not good at English, I will be grateful for your answers!
source
share