sequenced icon indicating copy to clipboard operation
sequenced copied to clipboard

Adding sequential_id to has_one :through relation

Open uxxman opened this issue 5 years ago • 4 comments

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?

uxxman avatar Feb 19 '20 14:02 uxxman

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

jdsampayo avatar Aug 08 '20 20:08 jdsampayo

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

Yes, it was a typo. But the issue remains

uxxman avatar Aug 09 '20 02:08 uxxman

@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.

phylor avatar Jan 28 '22 12:01 phylor

@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?

excid3 avatar Aug 15 '22 19:08 excid3