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)
source share