使用多个连接在Rails 3.2中编写一个简单的SQL查询

时间:2017-01-23 17:04:15

标签: ruby-on-rails postgresql activerecord

我需要在Rails 3.2语法中编写一个SQL查询,其中包含:

" 向我提交属于visibility_private == false "

轨道的任务的所有提交内容
Track.rb
   has_many :missions
   attr_accessible :visibility_private

Mission.rb
   belongs_to :track
   has_many :submissions

Submission.rb
   belongs_to :mission

我最初的尝试就是这个,但那肯定是错的:

Submission.joins(:mission).where(:missions => { mission.track.visibility_private == false })

2 个答案:

答案 0 :(得分:0)

你需要使用类似的东西,

Submission.joins(mission: :track).where("tracks.visibility_private = ?",false)

Submission.joins(mission: :track).where(tracks: { visibility_private: false })

希望这对你有用。

答案 1 :(得分:0)

尝试此代码

Submission.includes(mission: :track).where(tracks: { visibility_private: false })