user.as_json(
include: [
user_purchased_packages: {
include: [
business_package: {
include: [
business: {
include: :business_address
},
package: {
include: :services
}
]
}
]
}
]
)
as I passed in the array
[:business=>{:include=>:business_address},:package=>{:include=>:services}]
So it is expecting that the business and package both objects values should come.
but I am only able to get the business object and package object is not coming.
user.as_json(include: { :user_bookings=>{include: [:business_category_service_sub_services,:business => {include: :business_address}], methods: [:date_format]}, :user_purchased_packages=>{:include=>[:business_package=>{:include=>{:package => {include: :services} , :business=>{include: :business_address} } } ] , methods: [:date_format] } }, methods: [:followers_count,:following_count,:full_name])
Related
I have the following schema
class User
include Mongoid::Timestamps::CamelCaseCreated
include Mongoid::Timestamps::CamelCaseUpdated
embeds_many :intervals
field :name, type: String
# ...
end
class Interval
embedded_in :user
field :startTime, type: DateTime
field :endTime, type: DateTime
# ...
end
I need to get users where at least one interval has duration greater or equal to 4 hours.
So for the following data I should receive only Walter White
users = [
{
name: 'Walter White',
intervals: [
{ startTime: '2021-01-27T08:00:00Z', endTime: '2021-01-27T16:00:00Z' }
]
},
{
name: 'Jesse Pinkman',
intervals: [
{ startTime: '2021-01-27T08:00:00Z', endTime: '2021-01-27T10:00:00Z' }
{ startTime: '2021-01-27T10:00:00Z', endTime: '2021-01-27T13:00:00Z' }
]
},
{
name: 'Saul Goodman',
intervals: []
}
]
I know that on a document itself I could filter by computed field like this
User.collection.aggregate([
{ '$project' => { 'durationInMilliseconds' => { '$subtract' => ['$updatedAt', '$createdAt'] } } },
{ '$match' => { 'durationInMilliseconds' => { '$gte' => four_hours } } }
])
# or
User.where('$expr' => { '$gt' => [{ '$subtract': ['$updatedAt', '$createdAt']}, four_hours] })
I was hoping that those would work with $elemMatch, but I receive the following errors
User.where(
intervals: {
'$elemMatch' => [
{ '$project' => { 'durationInMilliseconds' => { '$subtract' => ['$endTime', '$startTime'] } } },
{ '$match' => { 'durationInMilliseconds' => { '$gt' => four_hours } } }
]
}
)
=> $elemMatch needs an Object
User.where(
intervals: {
'$elemMatch' => {
{ '$expr' => { '$max' => some_logic_here } },
}
}
)
=> $expr can only be applied to the top-level document
Is there a way to make it work with the embedded collection?
versions that I'm using:
gem mongo (2.10.2)
gem mongoid (6.1.1)
mongo --version - 4.2.9
I have following two array of hashes. I am trying to remove the record from doctor array hash whose doctor_patient_id doesnt not exist in doctor_patient_id array of patient_and_doctor array of hash.
doctor = [
{ :doctor_patient_id=>"abc",
:doctor_id=>"d1"
},
{ :doctor_patient_id=>"def",
:doctor_id=>"d2"
},
{ :doctor_patient_id=>"ghi",
:doctor_id=>"d3"
}
]
patient_and_doctor = [
{ :patient_id=>"11e8f37477ab7028a66b210b9699def9",
:doctor_patient_id=>[ "def", "zkj", "cps" ]
},
{ :patient_id=>"11e8f37481fabfe68630f5da2e22dceb",
:doctor_patient_id=>[ "uio", "ghi", "jkk" ]
}
]
expected output is:
doctor = [
{ :doctor_patient_id=>"def",
:doctor_id=>”d2”
},
{ :doctor_patient_id=>"ghi",
:doctor_id=>”d3”
}
]
I tried to do something like below but no luck,
patient_and_doctor.each do |dp|
data = doctor.map {|d| d[:doctor_patient_id].include?
dp[:doctor_patient_id] }
end
How can i achieve this?
valid_ids = patient_and_doctor.flat_map { |h| h[:doctor_patient_id] }
# => ["def", "zkj", "cps", "uio", "ghi", "jkk"]
doctor.select { |h| valid_ids.include? h[:doctor_patient_id] }
# => [{:doctor_patient_id=>"def", :doctor_id=>"d2"},
# {:doctor_patient_id=>"ghi", :doctor_id=>"d3"}]
use select! instead of select if you wish to mutate your doctor array instead of returning a new one.
Following can get required answer,
doctor.select { |x| patient_and_doctor.map { |x| x[:doctor_patient_id] }.flatten.include?(x[:doctor_patient_id]) }
I'm trying to make a "not simple" query using ActiveRecord:
ChatRoom.first.as_json(include: {
chat_room_members: {
include:{
user: {
include: [
hero_page: {
only: [:torch_id]
},
card: {
only: [:crop_y]
}
]
}
}}})
In the model, a ChatRoom has many ChatRoomMembers, that has an User, that has a HeroPage and a Card.
The problem is that ActiveRecord completely ignores the argument card. More specifically, ignores the all arguments after the first argument inside user: include{}:
{
"id" =>22,
"chat_room_members" => [
{
"id" =>7,
"user" => {
"id" =>22,
"hero_page" => {
"torch_id" =>"superhero23"
},
}
}
]
}
But if I remove the only argument from either hero_page or card, ActiveRecord show everything fine. Exemple:
[...]
include: [
hero_page: {
only: [:torch_id]
},
:card
]
[...]
Other weird fact is that I can type anything (respecting syntax) in the second argument and in causes no error. Example:
[...]
include: [
hero_page: {
only: [:torch_id]
},
this: {
only: [:doesnt, :cause, :error]
}
]
[...]
Just like in the first example, shows only hero_page and ignore the other parameter, this, that doesn't even exists.
Does someone know why the second argument is ignored in those cases??
user.include needs to be hash not an Array as you have done currently.
I connect Ember to a Rails API which delivers some JSON.
I'm trying to get relations working (product hasMany images) but it keeps giving me this error:
Cannot read property 'typeKey' of undefined
My Models:
App.Product = DS.Model.extend({
images: DS.hasMany('image'),
title: DS.attr('string'),
});
App.Image = DS.Model.extend({
product: DS.belongsTo('product')
});
Rails renders json as:
{
"products":[
{
"id": 1,
"title": "product title",
"images":[
{
"id": 1,
"urls":
{
"thumb":"http://domain.com/thumb/image.jpg",
"original":"http://domain.com/original/image.jpg"
}
}
]
}
]
}
Turns out I needed to "sideload" my images in Rails, so the JSON became:
{
"products":[
{
"id": 1,
"title": "product title",
"image_ids": [1]
}
],
"images":[
{
"id": 1,
"urls":
{
"thumb":"http://domain.com/thumb/image.jpg",
"original":"http://domain.com/original/image.jpg"
}
}
]
}
Rails' ProductSerializer:
class ProductSerializer < ActiveModel::Serializer
embed :ids, :include => true
attributes :id, :title
has_many :images
methods :image_urls
end
It seems that you were using embedded JSON in your example. You need to use the EmbeddedRecordsMixin https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/embedded_records_mixin.js and set the appropriate flag to mark images as being embededd
I need to figure if there are better ways to count the number of elements in an array that is like 5 levels deep.
I am trying to get a count of number of reports.
Here are my methods:
def member_report_sum
members.sum(&:report_sum)
end
def report_sum
member.information.groups.sum { |group| group.tasks.reports.count }
end
This is my structure:
members: [
{
id: 'member_id',
information: {
groups: [
{
name: 'Member1',
tasks: {
reports: [
{
name: 'Report 1',
}
]
}
},
{
name: 'Member 2',
tasks: {
reports: [
{
name: 'Report2',
}
]
}
}
]
}
}
}
Is there alternate ways to implement the above?