Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relationship scope #448

Open
kapso opened this issue Nov 6, 2019 · 1 comment
Open

Relationship scope #448

kapso opened this issue Nov 6, 2019 · 1 comment

Comments

@kapso
Copy link

kapso commented Nov 6, 2019

Is there a way to add scope to relationship, so something like this

Serializer:

class CompanySerializer
  include FastJsonapi::ObjectSerializer

  has_many :employees_temp, scope: ->(record) { record.employees.temp }, serializer: EmployeeSerializer
  has_many :employees_full_time, scope: ->(record) { record.employees.full_time }, serializer: EmployeeSerializer
end

Given the following models:

class Company < ApplicationRecord
  has_many :employees
end

class Employee < ApplicationRecord
  scope :temp, -> { where(type: 'temp') }
  scope :full_time, -> { where(type: 'full_time') }
end
@kapilnarula
Copy link

kapilnarula commented Nov 6, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants