Я создаю приложение «Список фильмов», в котором фильмы могут принадлежать многим спискам, а в списках много фильмов.Я хочу использовать таблицу соединений, чтобы связать эти модели списка и фильма.У меня проблемы с созданием этой ассоциации в консоли.
При попытке ввести этот код в консоли:
@bourne = Movie.create(title: "The Bourne Identity")
@list = List.create(name: "Bourne Films")
@list.movies << @bourne
Я получаю следующую ошибку:
NoMethodError: undefined method `movies' for #<List:0x007fe2ae380890>
Здесьмои модели и схемы:
class List < ApplicationRecord
validates :name, presence: true
has_many :list_movies
has_many :movies, through: :list_movies
end
class Movie < ApplicationRecord
has_many :list_movies
has_many :lists, through: :list_movies
end
class ListMovie < ApplicationRecord
belongs_to :list
belongs_to :movie
end
Моя схема:
create_table "lists", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "list_movies", id: false, force: :cascade do |t|
t.bigint "list_id", null: false
t.bigint "movie_id", null: false
end
create_table "movies", force: :cascade do |t|
t.string "title"
t.string "poster"
t.integer "year"
t.integer "runtime"
t.string "genre"
t.string "director"
t.string "plot"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Я что-то упустил?Это лучший способ справиться с этим?Заранее спасибо.