Skip to content

Instantly share code, notes, and snippets.

@adriangohjw
Created June 21, 2021 06:49
Show Gist options
  • Save adriangohjw/00f97a37cb5dcb5df5e25a21132086a4 to your computer and use it in GitHub Desktop.
Save adriangohjw/00f97a37cb5dcb5df5e25a21132086a4 to your computer and use it in GitHub Desktop.
Rails: Scenic gem for Database Views
class UserSalary < ApplicationRecord
attr_reader :a_but_different_name,
:b,
:c
end
class JobListing < ApplicationRecord
attr_reader :a,
:b_but_different_name,
:c
end
class MyCareersFutureJob < ApplicationRecord
attr_reader :a,
:b,
:c_but_different_name
end
class Salary < ApplicationRecord
attr_reader :a, :b, :c
end
class Salary
attr_reader :a, :b, :c
def initialize(data)
case data
when UserSalary
@a = data.a_but_different_name
@b = data.b
@c = data.c
when JobListing
@a = data.a
@b = data.b_but_different_name
@c = data.c
when MyCareersFutureJob
@a = data.a
@b = data.b
@c = data.c_but_different_name
end
end
end
SELECT
a_but_different_name AS "a",
b,
c
FROM user_salaries
UNION
SELECT
a,
b_but_different_name AS "b",
c
FROM job_listings
UNION
SELECT
a,
b,
c_but_different_name AS "c"
FROM my_careers_future_jobs
@adriangohjw
Copy link
Author

@adriangohjw
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment