ShiftNote belongs_to Shift has_many ShiftNote
我想有范围ShiftNote.by_month("2013-10")
如何实施?
现在我正在尝试:
ShiftNote.includes(:shift)
          .where("shifts.starts_at <= ? AND shifts.starts_at >= ?", "2013-10-01".to_date, "2013-10-30".to_date)
但我明白了
ShiftNote.includes(:shift).where("shifts.starts_at <= ? AND shifts.starts_at >= ?", "2013-10-01".to_date, "2013-10-30".to_date) 弃用警告:它看起来您正在急切地加载在字符串 SQL 片段中引用的表(其中之一:shift_notes、shifts)。例如:
Post.includes(:comments).where("comments.title = 'foo'")目前,Active Record 识别字符串中的表,并知道将评论表加入查询,而不是在单独的查询中加载评论。然而,在不编写成熟的 SQL 解析器的情况下这样做是有内在缺陷的。由于我们不想编写 SQL 解析器,因此我们将删除此功能。从现在开始,当你从一个字符串中引用一个表时,你必须明确地告诉 Active Record:
Post.includes(:comments).where("comments.title = 'foo'").references(:comments)如果您不依赖隐式连接引用,您可以通过设置完全禁用该功能
config.active_record.disable_implicit_join_references = true。SQL(1.6 毫秒)选择shift_notes。id作为 t0_r0,shift_notes.state作为 t0_r1,shift_notes.user_id作为 t0_r2,shift_days.shift_id作为 t0_r3,shift_notes.created_at作为 t0_r4,shift_notes.updated_at作为 t0_r5,shifts.id作为 t1_r0,shifts.starts_at作为 t1_r1shifts,。ends_at作为 t1_r2shifts,。rate作为 t1_r3,shifts。limit作为 t1_r4,shifts。created_at作为 t1_r5,shifts。updated_at作为 t1_r6shifts,。state作为 t1_r7,shifts.shift_notes_countAS t1_r8 从左外shift_notes连接shifts打开shifts。id=shift_notes。shift_id在哪里(shifts.starts_at <= '2013-10-01' AND shifts.starts_at >= '2013-10-30')