У меня есть 3 модели:Лучший способ заказать по ассоциациям
class Post
has_many :comments
end
class Comment
belongs_to :user
belongs_to :post
end
class User
has_many :comments
end
Теперь в контроллере, которые я хотел бы назвать @ post.comments и заказать эти комментарии по user.postcode. Я попытался следующие, но это не сработало:
class Post
has_many :comments, :order => "user.postcode"
end
Я также попытался:
class Comment
def order_by_user_postcode
includes(:user).order("user.postcode ASC")
end
end
class PostsController
@post.comments.order_by_user_postcode
end
что приводит к
undefined method for ActiveRecord::Relation
Как я могу написать метод для цепи @post .comments для сортировки по user.postcode?
попытка 'присоединяется (: пользователь) .order ("users.postcode") ' – Zippie