Home > front end >  I don't understand the new and create associations in rails
I don't understand the new and create associations in rails

Time:10-05

What I want to achieve

I have created an association between movies and schedules, but I don't know how to instantiate the association, even after reading the Rails Guide.

Code

db/schema


# movies
  create_table "movies", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.string "name"
    t.integer "year"
    t.string "description"
    t.string "image_url"
    t.integer "is_showing"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

#schedules
  create_table "schedules", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
    t.bigint "movie_id", null: false
    t.time "start_time", null: false
    t.time "end_time", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["movie_id"], name: "index_schedules_on_movie_id"
  end

model

movie.rb

class Movie < ApplicationRecord
  has_many :schedules
end

schedule.rb

class Schedule < ApplicationRecord
    belongs_to :movie
end

controller

schedules

class SchedulesController < ApplicationController
  def index
    @movies = Movie.joins(:schedules).select("movies.*", "schedules.*")
  end

###############################################################################

# I don't get it.
  def new
    @schedule = @movie.build_schedules()
  end


  def create
    @schedule = Schedule.new(schedule_params) 
    @schedule.save
    redirect_to schedules_path
  end
###############################################################################



  private
    def schedule_params
      params.require(:schedule).permit(:start_time, :end_time)
    end  
end
 

new.html.erb


<!DOCTYPE html>
<html lang="en">
  <head>
    <%= render 'shared/head' %>
    <title>schedule/new</title>
  </head>
    <body>
      <%= form_with model: @schedule, url: movie_schedules_path do |form| %>
        <div class="field">
          <%= form.label :start_time %>
          <%= form.date_field :start_time %>
        </div>
        <div class="field">
          <%= form.label :end_time %>  
          <%= form.date_field :end_time %>
          <%= form.submit %>
        </div>
      <% end %>
    </body>
</html>

What I've tried.

・Check the Rails Guide. I was using build instead of new, but it took an argument. I didn't know what I needed for the argument part here.

# Rails Guide
@book.author = @author
@author = @book.build_author(author_number: 123,
                                  author_name: "John Doe")

CodePudding user response:

You want to do @movie.schedules.build to instantiate a new record for the @movie.schedules association because it's a has_many.

You can also do @movie.schedules.create(schedule_params) to create the Schedule associated to the Movie instance in one step.

record.build_xxx only works for has_many/belongs_to associations (you could do some_schedule.build_movie for example).

  • Related