How to filter by alien identifier and local attribute via own_to?

The following models are linked through belongs_to :

 require 'mongoid' class Sensor include Mongoid::Document field :sensor_id, type: String validates_uniqueness_of :sensor_id end 

...

 require 'mongoid' require_relative 'sensor.rb' class SensorData include Mongoid::Document belongs_to :sensor field :date, type: Date field :ozonMax1h, type: Float field :ozonMax8hMittel, type: Float index({ date: 1, sensor_id: 1 }, { unique: true }) end 

Here is a Sinatra application that provides several API paths based on these models:

 require 'sinatra' require 'csv' require_relative './models/sensor.rb' require_relative './models/sensor_data.rb' configure do Mongoid.load!('./mongoid.yml') end def prepare_for_export(sensor_data) converted_data = sensor_data.asc(:date).map do |e| { sensor_id: e.sensor.nil? ? :null : e.sensor.sensor_id, date: e.date, ozonMax1h: e.ozonMax1h, ozonMax8hMittel: e.ozonMax8hMittel } end converted_data end def convert_to_json(sensor_data) prepare_for_export(sensor_data).to_json end def convert_to_csv(sensor_data) data = prepare_for_export sensor_data csv_string = CSV.generate do |csv| csv << data.first.keys data.each do |hash| csv << hash.values end end csv_string end def get_recent max_date = SensorData.max(:date) SensorData.where(date: max_date) end def get_for_year(year) SensorData.where(:date.gte => Date.new(year, 1, 1)).where(:date.lte => Date.new(year, 12, 31)) end def get_for_sensor(sensor) foo = SensorData.where(sensor_id: sensor) puts "hallo" return foo end get '/api/v1/stations' do content_type :json Sensor.all.map { |e| {sensor_id: e.sensor_id} }.to_json end get '/api/v1/sensordata/:year' do content_type :json convert_to_json get_for_year(params[:year].to_i) end get '/api/v1/sensordata/:year/csv' do convert_to_csv get_for_year(params[:year].to_i) end get '/api/v1/recent' do content_type :json convert_to_json get_recent end 

I would like to output SensorData for a specific sensor, for example, here:

 /api/v1/stations/:sensor_id/sensordata/:year/csv 
+6
source share
1 answer

I'm not sure what you are trying to do, or even if you are still looking for an answer, but here it goes. Something seems wrong with the models in the example that you have here. It seems that part of what you are doing will work if Sensor knows about sensor_data . Therefore, you may need to add this to the Sensor class:

 has_many :sensor_data 

Although the singularity of the data is a database. The class is SensorDatum be SensorDatum . If you cannot change it, you need to tell Mongoid that class_name expects the SensorData key to be pressed in has_many .

You can specify foreign_key in Mongoid using belongs_to .

You cannot filter with belongs_to as you can with ActiveRecord, but you can use areas outside belongs_to to get the same effect. Exampe:

 belongs_to :sensor scope :for_year, -> (year) { where(:date.gte => Date.new(2015,1,1)).where(:date.lte => Date.new(2015, 12, 31))} 

or

 belongs_to :sensor def self.for_year year where(:date.gte => Date.new(year,1,1)).where(:date.lte => Date.new(year, 12, 31)) end 

So your query will become something like this:

 sensor = Sensor.find_by(sensor_id: params[:sensor_id]) sensor.sensor_data.for_year(2015) 
+1
source

Source: https://habr.com/ru/post/989536/


All Articles