Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do i create these associations so they are sortable?

Twig asked in General

I need some help figuring out how I should associate my models.

I have three models:

class Series < ApplicationRecord
  has_many :modules
  has_many :episodes
end
class Module < ApplicationRecord
  belongs_to :series
  has_many :episodes
end
class Episode < ApplicationRecord
  belongs_to :modules, optional: true
  belongs_to :series, optional: true
end

I need to be able to manually specify the order of the following has_many relationships:

  1. Module in a Series
  2. Episode in a Module
  3. Episode in a Series.

I believe I need a has_many, through:, possibly 3 of them? One for each relationship, so

class Series < ApplicationRecord
  has_many :modules, through: :module_series
  has_many :episodes, through: :episode_series
end
class Module < ApplicationRecord
  belongs_to :series
  has_many :episodes, through: module_episode
end

Am i approaching this the correct way?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.