Rails的加入限制了关联

最高

我正在尝试查询所有测,并加入测度

但是我只想要最近的度量(由created_at DESC排序),因为一个台站有成千上万的度量。

我试过了

Station.joins(:measures).limit(1) 

但这仅限制了站点。

附加信息:

车站有很多措施

措施属于车站

我已经阅读了Active Records文档,只有关于在关联上使用where条件的信息。

该应用程序仅针对Postgres,接受SQL。


编辑:添加从schema.rb除外:

  create_table "measures", force: true do |t|
    t.integer  "station_id"
    t.float    "speed"
    t.float    "direction"
    t.float    "max_wind_speed"
    t.float    "min_wind_speed"
    t.float    "temperature"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.float    "speed_calibration"
  end

  add_index "observations", ["created_at"], name: "index_observations_on_created_at", using: :btree
  add_index "observations", ["station_id"], name: "index_observations_on_station_id", using: :btree

  create_table "stations", force: true do |t|
    t.string   "name"
    t.string   "hw_id"
    t.float    "latitude"
    t.float    "longitude"
    t.float    "balance"
    t.boolean  "offline"
    t.string   "timezone"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "slug"
    t.boolean  "show",                         default: true
    t.float    "speed_calibration",            default: 1.0
    t.datetime "last_observation_received_at"
  end

另外,这是当前正在使用的非常简单的代码:

def all_with_latest_measure
  if user_signed_in? && current_user.has_role?(:admin)
    stations = Station.all.load
  end
  stations ||= Station.where(show: true).load

  if stations.size
    ids = stations.map { |s| s.id }.join(',')
    where = "WHERE m.station_id IN(#{ids})" unless ids.empty?
    measures = Measure.find_by_sql(%Q{
    SELECT DISTINCT ON(m.station_id, m.created_at)
      m.*
    FROM measures m
    #{where}
    ORDER BY m.created_at DESC
    })

    stations.each do |station|
      # Setup has_many relationship between station and Measure
      # Prevents n+1 queries
      measure = Measures.find { |m| m.station_id == station.id  }
      if measure
        measure.station = station
        station.latest_measure = measure
      end
    end
  end
end
联合收割机

我相信在Rails 4中,您可以在关联上应用范围:

class Stations
  has_many :measures, -> { order('created_at DESC').limit(1)  }
end

然后:

2.0.0-p353 :008 > Station.first.measures
  Station Load (0.1ms)  SELECT "stations".* FROM "stations" ORDER BY "stations"."id" ASC LIMIT 1
  Measure Load (0.1ms)  SELECT "measures".* FROM "measures" WHERE "measures"."station_id" = ? ORDER BY created_at DESC LIMIT 1  [["station_id", 1]]

编辑:实际上,如果您只需要最新的,就可以使用has_one稍作修改后的语法即可在Rails 4和Rails 3上使用:

class Stations
  has_one :recent_measure, -> { order('created_at DESC')  }, class_name: 'Measure' # Rails 4
  has_one :recent_measure, order: 'created_at DESC', class_name: 'Measure' # Rails 3
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails的加入限制了关联

来自分类Dev

Rails自引用关联加入条件

来自分类Dev

Rails自引用关联加入条件

来自分类Dev

限制Rails中的最大关联

来自分类Dev

如何通过 rails 中的关联限制结果

来自分类Dev

Rails 4-限制从关联模型中检索的列

来自分类Dev

Rails 4:通过关联限制字段选择

来自分类Dev

加入单向关联

来自分类Dev

如果关联具有限制子句,则关联关系属性上的 Rails 总和不正确

来自分类Dev

MySQL更新加入限制

来自分类Dev

PostgreSQL加入限制

来自分类Dev

如何在Ruby on Rails的ActiveRecord查询中加入间接关联?

来自分类Dev

Rails 按当前用户设置过滤器 - 加入关联问题

来自分类Dev

Rails关联关联

来自分类Dev

Rails:获取关联关联

来自分类Dev

极限限制包括关联

来自分类Dev

与Sequelize的belongsToMany关联的限制

来自分类Dev

将模型验证限制为仅与Rails 3.2中与current_user关联的记录

来自分类Dev

MySQL更新与加入和限制

来自分类Dev

Rails-加入条件

来自分类Dev

Rails ActiveRecord加入混乱

来自分类Dev

Rails:自我加入

来自分类Dev

与用户的自我加入关联

来自分类Dev

在多态方案中加入关联

来自分类Dev

加入涉及多个关联的集成查询

来自分类Dev

MYSQL加入。如何只参加首场比赛?(加入限制1)

来自分类Dev

Rails中的简单关联

来自分类Dev

Rails多态关联参考

来自分类Dev

Rails:包含多态关联