sci*_*fic 4 scope ruby-on-rails associations
在我的 Rails 应用程序中,项目可以包含视频。我想为项目创建一个范围以获取没有关联视频的项目。我该怎么做呢?
这是我的模型协会
class Project < ActiveRecord::Base
has_one :video
end
class Video < ActiveRecord::Base
belongs_to :project
end
Run Code Online (Sandbox Code Playgroud)
现在刚刚遇到这个问题,这对我有用:
Project.includes(:video).where(videos: { id: nil })
Run Code Online (Sandbox Code Playgroud)
技巧是使用单数 inincludes和复数 inwhere