sequenced
sequenced copied to clipboard
Adding sequential_id to has_one :through relation
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?
It appears your last class should be Account and not Customer doesn't?
It appears your last class should be Account and not Customer doesn't?
Yes, it was a typo. But the issue remains
@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 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?