ActiveRecord哪里Assoc
使用此gem可以轻松地根据ActiveRecord(Rails)中记录的关联来进行条件。 (使用SQL的EXISTS运算符)
# Find my_post's comments that were not made by an admin
my_post . comments . where_assoc_not_exists ( :author , is_admin : true ) . where ( ... )
# Find every posts that have comments by an admin
Post . where_assoc_exists ( [ :comments , :author ] , & :admins ) . where ( ... )
# Find my_user's posts that have at least 5 non-spam comments (not_spam is a scope on comments)
my_user . posts . where_assoc_count ( 5
1