class Tourist < ApplicationRecord
has_many :visits
has_many :countries, through: :visits
end
class Country < ApplicationRecord
has_many :visits
has_many :tourists, through: :visits
end
class Visit < ApplicationRecord
belongs_to :country
belongs_to :tourist
end
我正在设法按旅游协会的数目把这些国家分类。
Country.joins(:tourist).group(:tourist_id).order('COUNT(*)')
然而,这似乎并不完全正确。首先,我得到一个折旧警告。
DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with a non-attribute argument(s): "COUNT(*)". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). (called from __pry__ at (pry):61)
而且,我不认为我得到了正确的值,因为
first
是零。
什么是获得关联最多的记录的正确方法?