Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails中使用嵌套的where条件_Ruby On Rails_Conditional Statements_Nested Attributes - Fatal编程技术网

Ruby on rails 如何在rails中使用嵌套的where条件

Ruby on rails 如何在rails中使用嵌套的where条件,ruby-on-rails,conditional-statements,nested-attributes,Ruby On Rails,Conditional Statements,Nested Attributes,不仅可以应用多个where条件,还可以应用嵌套条件吗 比如: conversations.where.not(:sender\u id=>current\u user.id).messages.last.content 我不仅要访问发件人不是当前用户的对话,还要访问与对话关联的嵌套消息 可能吗?谢谢您的回复,您能解释一下您的代码吗messages.joins(:conversation).where.not(conversations:{sender\u id:current\u user.id

不仅可以应用多个where条件,还可以应用嵌套条件吗

比如:
conversations.where.not(:sender\u id=>current\u user.id).messages.last.content

我不仅要访问发件人不是当前用户的对话,还要访问与对话关联的嵌套消息


可能吗?

谢谢您的回复,您能解释一下您的代码吗
messages.joins(:conversation).where.not(conversations:{sender\u id:current\u user.id}).最后一个.body
在我的代码中是不可能的,我假设
消息属于:conversation
并且
conversation有很多:消息
。如果这是真的,它应该适用于您的案例关联是正确的,但如果我按照您的建议尝试,则当
当前用户.messages.joins(:conversation).where.not(conversations:{sender\u id:current\u user.id})
给我一个ActiveRecord\u关联关系时,没有可用的消息,我无法获得实际的数据content@GabrielTheCoder
ActiveRecord\u AssociationRelation
意味着您可以获得ActiveRecord的集合。这意味着它正在工作。现在只需执行您要求的操作-
.last.content
.last.content
返回一个无方法错误,即使有一列称为“content”。Just
。last
不返回任何对象
Message
  .joins(:conversation)
  .where.not(conversations: { sender_id: current_user.id })
  .last
  .content