【问题标题】:Rails: HABTM - find all records with no associationRails:HABTM - 查找所有没有关联的记录
【发布时间】:2012-07-01 03:23:13
【问题描述】:

我有 2 个模型(锻炼、设备),属于多个关系。如果我使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = 5") 它可以工作,但如果我使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = null") 它不会返回没有关联的记录。有什么想法吗?

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 has-and-belongs-to-many


【解决方案1】:

试一试;

Workout.joins("left join equipments e on workouts.id = e.workouts_id").where("e.id is null")

【讨论】:

  • rails 3 arel: Workout.joins(:equipments, Arel::Nodes::OuterJoin).where( Equipment.arel_table[:id].eq(nil) )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 2014-12-08
  • 2023-03-20
  • 2016-08-30
  • 2016-08-12
相关资源
最近更新 更多