I have a Rails4 app which has many models and many has_many
.Now when i am eager loading using Model.includes(:other_models)
, i want the query to execute only when all the includes are persisted or present, but i am unable to do so.
for example -
in my shop.rb
has_one :announcement, :dependent => :destroy
has_many :pictures, -> { where.not('venue_id' => nil) }, class_name: 'Picture', :dependent => :destroy
has_many :ratings, -> { where(rateable_type: 'Shop') } , foreign_key: 'rateable_id' ,:class_name=>"Rate"
has_many :videos, :dependent => :destroy
has_many :testimonials, :dependent => :destroy
has_many :managers, :dependent => :destroy
has_many :pricing_details, :dependent => :destroy
has_many :comments, :as=> :commentable, :class_name => 'Comment',:dependent => :destroy
has_many :user_inquiries, :dependent => :destroy
has_many :venue_checkers, :as=> :contactable, :class_name => 'VenueChecker',:dependent => :destroy
So now, if i want to load a Shop
, which should also have all the dependencies persisted
, i try this ...
Shop.includes(:contact_detail,address,pictures,videos,annoucement, :hall_request,:hall_facilities ,:event_categories....other models).find(1010)..
i get the Shop, if i remove the address or contact_detail or any other model, it still shows up, which i dont want.
i want a method that should only return the shop when it has all its has_many dependencies persisted else return nothing.
i want something like this..
def get_completed_shops_only
Shope.distinct.includes(:hall_request,:hall_facilities ,:event_categories, :address,:contact_detail,:pictures,:videos,:ratings....other models)
end
### Shop.get_completed_shops_only.find(100) - must return a shop with all dependencies present else nothing.
I know includes puts a Left join
, which matches all for left table and right table if it exists, but how can i do it here.I can do the same with joins
but that will hit performance.I just want to includes persisted items only.
Kindly help
Aucun commentaire:
Enregistrer un commentaire