0

I'm looking into nested attributes and I have a few questions about relationships between models.

Say I have a Show that has_many Seasons. Both Show and Season can have many Actors and Staff. See table example:

enter image description here

When a Show is created, Season accepts the Show association and both Actors and Staff accept both the Show and Season attributes. Would this be a triple nested form?

So my models would look like this?

class Show < ApplicationRecord
  has_many :seasons, dependent: :destroy
  has_many :actors, dependent: :destroy
  has_many :staff, dependent: :destroy

  accepts_nested_attributes_for :seasons, allow_destroy: true
  accepts_nested_attributes_for :actors, allow_destroy: true
  accepts_nested_attributes_for :staff, allow_destroy: true
end

class Season < ApplicationRecord
  belongs_to :show
  has_many :actors, dependent: :destroy
  has_many :staffs, dependent: :destroy

  accepts_nested_attributes_for :actors, allow_destroy: true
  accepts_nested_attributes_for :staff, allow_destroy: true
end

class Actor < ApplicationRecord
  belongs_to :show
  belongs_to :season
end

class Staff < ApplicationRecord
  belongs_to :show
  belongs_to :season
end

And my Show Controller would look like:

class ShowsController < ApplicationController
  def create
   @show.seasons.build
   @show.seasons.build.actors.build
   @show.seasons.build.staffs.build
  end
end
7
  • If I understand the problem correctly, when Show is created you also want to create a Season (one) for this Show, and Actors (many) and Staff (many), correct? Commented Oct 20, 2018 at 1:45
  • Yes, thats correct. Commented Oct 20, 2018 at 1:45
  • Ok, thanks. Your data schema is incorrect then. In code model Actor has many shows and has many seasons. The same for Staff. But above the schema you wrote: Both Show and Season can have many Actors and Staff Commented Oct 20, 2018 at 1:48
  • Ok. So I have updated my models above. Is this correct? And to build the Actor and Staff nested attributes do I do what's in the Show Controller? Commented Oct 20, 2018 at 1:54
  • Oh, I didn't see your comment and wrote an answer. Yes, now it is correct Commented Oct 20, 2018 at 1:57

1 Answer 1

1

Correct schema is:

class Show < ApplicationRecord
  has_many :seasons, dependent: :destroy
  has_many :actors, dependent: :destroy
  has_many :staff, dependent: :destroy

  accepts_nested_attributes_for :seasons, allow_destroy: true
  accepts_nested_attributes_for :actors, allow_destroy: true
  accepts_nested_attributes_for :staff, allow_destroy: true
end

class Season < ApplicationRecord
  belongs_to :show
end

class Actor < ApplicationRecord
  belongs_to :show
  belongs_to :season
end

class Staff < ApplicationRecord
  belongs_to :show
  belongs_to :season
end

The rule is simple - if table objects has a foreign key subject_id, which links it to table subjects, then model Object may contain belongs_to :subject association.

There is a nice explanation about which relationship to use in your model depending on foreign_key placement.

Off-topic:

I don't know the specifics of your project, but it looks a bit offensive. You create new actors and staff each time a new show is created like they are expendable. I do not want to seem cynical, but the actors and the staff can be reused. If so, it could be reasonable to create has_and_belongs_to_many relationships.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the clarification. Appreciate it.
I have another question about these nested forms, similar to this, but with a a trickier outcome if you have the time?
Yes, sure, what's the question?
Should I open a new question?
No, you can write here. It's more convenient to open a chat, however I cannot find how to do it :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.