Skip to content

Instantly share code, notes, and snippets.

@andhart
Created August 25, 2021 17:54
Show Gist options
  • Save andhart/cbca5c6287c2cb7d03323f5d7d797a5d to your computer and use it in GitHub Desktop.
Save andhart/cbca5c6287c2cb7d03323f5d7d797a5d to your computer and use it in GitHub Desktop.
REQUIREMENTS:
- Query must be done using SQL (preferably utilizing ActiveRecord, but can also be raw SQL queries)
JOB DESCRIPTION:
We have a few RAILS models:
# company.rb (app/models)
class Company < ApplicationRecord
has_many :assets, dependent: :destroy
has_many :articles, dependent: :destroy
has_many :websites, dependent: :destroy
end
#photo.rb (app/models)
class Photo < ApplicationRecord
include PhotoUploader[:file]
include Discard::Model
belongs_to :photoable, polymorphic: true
end
#photoable.rb (app/models/concerns)
module Photoable
extend ActiveSupport::Concern
included do
has_many :photos, as: :photoable, dependent: :destroy
end
end
#asset.rb (app/models)
class Asset.rb < ApplicationRecord
include Photoable
belongs_to :company
end
#website.rb (app/models)
class Website.rb < ApplicationRecord
include Photoable
belongs_to :company
end
#article.rb (app/models)
class Article.rb < ApplicationRecord
include Photoable
belongs_to :company, optional: true
end
The goal is to write a query to list all the photos (that are not discarded) for a particular company.
Ideally, we would have added a photo.company_id field, but we can't go back now, and we need an alternative query.
Thank you!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment