I'm working on a application in which there is table named companies and a company can have multiple users and users can have multiple books and magazines belonging to company. Currently the associations is as follows:
Company.rb
has_many :users
has_many :books
has_many :magazines
User.rb
belongs_to :company
has_many :user_books, dependent: :destroy
has_many :books, through: :user_books
has_many :user_magazines, dependent: :destroy
has_many :magazines, through: :user_magazines
Book.rb
belongs_to :company
has_many :user_books, dependent: :destroy
has_many :users, through: :user_books
Same association exists for magazines as well. Not using has_and_belongs_to_many association as we are capturing additional data in association table.
Now we need to introduce a new entity called audio_books similar to books and magazines but instead of creating one more association table called user_audio_books we are planning to create a polymorphic association table called user_items which would record the association between user and the company items(books, magazines & audio_books).
New association: User.rb
has_many :user_items, dependent: destroy
has_many :books, through: :user_items, as: :source, source: source, source_type: 'Book'
has_many :magazines, through: :user_items, as: :source, source: source, source_type: 'Magazine'
However this new association will be enabled only for few companies for time being, until then we need to support all the 16 methods added by has_many association on both the models on conditional basis.
I'm looking for something like in user.rb:
has_many :user_books, dependent: :destroy
has_many :user_items, dependent: :destroy
<if company.audio_books_enabled? >
has_many :books, through: :user_items, as: :source, source: source, source_type: 'Book'
<else>
has_many :books, through: :user_books
<end>
audio_books_enabled? is not an sql query but a redis based check.
Is it possible to achieve this?