查找没有任何具有特定值的关联记录的记录

时间:2016-04-07 11:25:22

标签: ruby-on-rails activerecord

我有几个型号:User和UserTags

A User has_many UserTags
A UserTag belongs_to User

我正在尝试找到所有Users没有UserTag名称'记录' (所以我也想要那些根本没有任何标签的用户)。鉴于用户关系,我有这个:

users.joins("LEFT OUTER JOIN user_tags ON user_tags.user_id = users.id AND user_tags.name = 'recorded'").
where(user_tags: { id: nil })

还有其他更好或更多的Railsy方法吗?

5 个答案:

答案 0 :(得分:2)

试试这个:

users.joins("LEFT OUTER JOIN user_tags ON user_tags.user_id=users.id").where("user_tags.name != ? OR user_tags.id is NULL", 'recorded')

答案 1 :(得分:1)

这个应该有效:

    Sending value: hi of type class demo.domain.Foo
2016-04-07 19:19:50.811 ERROR 76016 --- [hannel-adapter1] o.s.c.s.b.r.RedisMessageChannelBinder$2  : Failed to deliver message; retries exhausted; message sent to queue 'ERRORS:testtock.anonymous.61fc01c4-17a1-46b5-9579-286413dc45e0' 

org.springframework.messaging.converter.MessageConversionException: No converter found to convert to class demo.domain.Bar, message=GenericMessage [payload=hi, headers={contentType=text/plain, id=72274970-5e79-a8ef-d3f1-fcd4ede2fa55, timestamp=1460027990799}]
    at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:118) ~[spring-messaging-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:98) ~[spring-messaging-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:138) ~[spring-messaging-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:107) ~[spring-messaging-4.2.5.RELEASE.jar!/:4.2.5.RELEASE]
    at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor$StreamListenerMessageHandler.handleRequestMessage(StreamListenerAnnotationBeanPostProcessor.java:192) ~[spring-cloud-stream-1.0.0.RC1.jar!/:1.0.0.RC1]
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99) ~[spring-integration-core-4.2.5.RELEASE.jar!/:na]

答案 2 :(得分:0)

试试这个,它将返回0 user_tags:

的用户
users = users.joins(:user_tag).where("users.id IN (?) OR user_tags.name != ?",User.joins(:user_tag).group("users.id").having('count("user_tag.user_id") = 0'), "recorded")

答案 3 :(得分:0)

加入不应该使用的加载嵌套模型"包含或eage_load"

 users.eager_load(:user_tags).where.not(user_tags: { name: 'recorded' })

这将使用左外连接,您可以在where子句中更新您的查询。 与

相同
users.includes(:user_tags).where.not(user_tags: { name: 'recorded' })

答案 4 :(得分:0)

嘿,您可以简单地使用includes进行外部联接,因为user_tags.id is null会返回所有没有user_tags的记录,user_tags.name != 'recorded'会返回记录,而user_tag名称不是{{1} }

recorded

或者您也可以使用users.includes(:user_tags).where("user_tags.id is null or user_tags.name != ?","recorded") 子句,但它不是给定查询的优化方式:

not in