没有依赖的列:destroy on has_many,belongs_to relationship

时间:2015-08-21 18:48:54

标签: ruby-on-rails ruby-on-rails-4

我有一个简单的has_many,belongs_to关联。

class Actor < ActiveRecord::Base
    belongs_to :movie
end

class Movie < ActiveRecord::Base
    has_many :actors, dependent: :destroy
    after_save :fill_actors_table
    validates_presence_of :title

    def fill_actors_table
       movie_list = Imdb::Search.new("Lion King")
      new_movie = movie_list.movies.first
      id = new_movie.id

      i = Imdb::Movie.new("#{id}")
      i.cast_members.each do |actor_name|
         actor_image = Google::Search::Image.new(:query => actor_name).first
         actor_image_url = actor_image.uri
         Actor.create(:name => actor_name, :file => actor_image_url)
      end
   end

我的架构如下所示:

ActiveRecord::Schema.define(version: 20150821182841) do

  create_table "actors", force: true do |t|
     t.string   "name"
     t.string   "file"
     t.integer  "actor_id",   limit: 255
     t.datetime "created_at"
     t.datetime "updated_at"
   end

  create_table "movies", force: true do |t|
     t.string   "title"
     t.datetime "created_at"
     t.datetime "updated_at"
   end
 end

但我一直收到错误

SQLite3::SQLException: no such column: actors.movie_id: SELECT "actors".* FROM "actors" WHERE "actors"."movie_id" = ?

我不会在任何地方使用movie_id !!!

电影控制器代码:

class MoviesController < ApplicationController
  before_action :set_movie, only: [:show, :edit, :update, :destroy]

  # GET /movies
  # GET /movies.json
  def index
    @movies = Movie.all
  end

  # GET /movies/1
  # GET /movies/1.json
  def show
  end

  # GET /movies/new
  def new
    @movie = Movie.new
  end

  # GET /movies/1/edit
  def edit
  end

  # POST /movies
  # POST /movies.json
  def create
    @movie = Movie.find_or_create_by(movie_params)

    respond_to do |format|
      if @movie.save
        format.html { redirect_to @movie, notice: 'Movie was successfully created.' }
        format.json { render :show, status: :created, location: @movie }
      else
        format.html { render :new }
        format.json { render json: @movie.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /movies/1
  # PATCH/PUT /movies/1.json
  def update
    respond_to do |format|
      if @movie.update(movie_params)
        format.html { redirect_to @movie, notice: 'Movie was successfully updated.' }
        format.json { render :show, status: :ok, location: @movie }
  else
        format.html { render :edit }
        format.json { render json: @movie.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /movies/1
  # DELETE /movies/1.json
  def destroy
    @movie.destroy
    respond_to do |format|
      format.html { redirect_to movies_url, notice: 'Movie was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_movie
      @movie = Movie.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def movie_params
      params.require(:movie).permit(:title)
    end


end

演员控制器刚刚生成,并且在很大程度上与电影控制器完全相同。

我想要完成的事情: 搜索电影。电影名称保存在电影数据库中。然后它使用imdb gem提取电影中的演员列表,并使用google-search gem搜索他们的图像。图像网址和参与者名称保存在actor数据库中。

我注意到当我放入电影时,有时似乎会将演员名称列出两次(好像有两个for循环)。我无法弄清楚我的代码在哪里可能会让它运行两次。

这是我在整个项目中编写的唯一代码,而不是基本形式。

1 个答案:

答案 0 :(得分:1)

您需要使用belongs_to。

在模型中使用外键

使用时:

belongs_to :some_model

rails假定:some_model_key在模型中。你的actor模型中没有:movie_id,所以当你尝试引用一部电影时,rails会在你的演员模型中寻找:movie_id并且无法找到它。您可以使用active migration.

添加此列