class Tree
has_many :tree_locations
has_many :countries, through: :tree_locations
has_many :native_tree_locations, class_name: 'TreeLocation',
-> { where(native: true) }
has_many :native_countries, through: :native_tree_locations,
class_name: 'country'
class TreeLocation
belongs_to :tree
belongs_to :country
# and a `native` boolean flag in the DB
class Country
has_many :tree_locations
has_many :trees, through: :tree_locations
# ...
1条答案
按热度按时间atmip9wb1#
我会这样建模:
这允许给定的
tree
:tree.countries
将返回树生长的所有国家/地区,但是tree.native_countries
将仅返回该树所在的国家/地区。