记录是使用默认范围添加的,但不是必需的。
class PostsTag
# published is false by default
end
class Post
has_many :posts_tags
{published: true, private: false}.each do |key, val|
has_many "#{key}_tags",
through: "posts_tags",
conditions: ["posts_tags.published = ?", val],
source: :tag
end
end
#--------------------
Post.first.published_tags << Tag.first
Post.first.published_tags.count # = 0
Post.first.private_tags.count # = 1
我做错了什么?感谢提前。