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

Adding sequential_id to has_one :through relation #45

Open
uxxman opened this issue Feb 19, 2020 · 4 comments
Open

Adding sequential_id to has_one :through relation #45

uxxman opened this issue Feb 19, 2020 · 4 comments

Comments

@uxxman
Copy link

uxxman commented Feb 19, 2020

Good Day :),

Relations:

class Bank < ApplicationRecord
  has_many :accounts
  has_many :customers, through: :accounts
end

class Customer < ApplicationRecord
  has_one :account
  has_one :bank, through: :account
end

class Account < ApplicationRecord
  belongs_to :bank
  belongs_to :customer
end

Problem

Since, there is no bank_id in customers table, how can I add sequential_id to Customer model and scope it the Bank model?

@jdsampayo
Copy link

It appears your last class should be Account and not Customer doesn't?

@uxxman
Copy link
Author

uxxman commented Aug 9, 2020

It appears your last class should be Account and not Customer doesn't?

Yes, it was a typo. But the issue remains

@phylor
Copy link

phylor commented Jan 28, 2022

@uxxman I'm having a similar requirement currently and created a prototype implementation. In your case, the following might work:

In your Gemfile:

gem 'sequenced', github: 'phylor/sequenced', branch: 'features/scope-across-relations'
# Customer
delegate :bank_id, to: :account

acts_as_sequenced scope: 'account.bank_id'

If there is interest of @derrickreimer or others, we could work on a pull request implementing this properly.

@excid3
Copy link
Collaborator

excid3 commented Aug 15, 2022

@phylor Send over a PR and a test and I'll get it merged and released.

I guess we'd want to account for possibly multiple join tables too?

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

No branches or pull requests

4 participants