I have one table cameras
and what options am available with is current_user.id
. In cameras table, every camera have an owner_id
which is actually the user's id who added the camera. Cameras for the current user can be get like this
user_cameras = Camera.where(owner_id: current_user.id).order(:id).all
But I have an other table camera_shares
in which there is an camera_id
and a user_id
(with whom the camera has been shared), What i am doing is, Getting all those shares first where the user_id
is current_user.id
and then get all those camera_id
's and through them I get cameras as
camera_shares = CameraShare.where(user_id: current_user.id).all
camera_share_ids = []
camera_shares.each do |share|
camera_share_ids[camera_share_ids.count] = share.user_id
end
@shared_cameras = Camera.where(id: camera_share_ids).order(:id).all
What I am looking is a way to make some join in the very first query where am getting user cameras, So that in one query I can get all the user owned cameras and as well as those which have been shared with him, As In camera_shares, I have the refernce of user_id
and also camera_id.
I that possible to solve all this mess in one single query?
UPDATE: Camera.rb details
class Camera < Sequel::Model
include Hashie::Extensions::Mash
many_to_one :vendor_model, class: 'VendorModel', key: :model_id
one_to_many :endpoints, class: 'CameraEndpoint'
many_to_one :owner, class: 'User', key: :owner_id
one_to_many :shares, class: 'CameraShare'
one_to_many :webhooks, class: 'Webhook'
one_to_one :cloud_recording
one_to_one :motion_detection
CameraShares details
class CameraShare < Sequel::Model
# Share kind constants.
PRIVATE = 'private'.freeze
PUBLIC = 'public'.freeze
ALL_KINDS = [PRIVATE, PUBLIC]
# Class relationships.
many_to_one :camera
many_to_one :user
many_to_one :sharer, class: 'User', key: :sharer_id
Try following
Assuming you have something like following in
camera.rb
EDITED