【问题标题】:rails 4 many to many though join model, undefined method 'name' for:NilClass, sqlite3rails 4 many to many but join model, undefined method 'name' for:NilClass, sqlite3
【发布时间】:2015-03-31 12:14:05
【问题描述】:

大家好,我通过 WatchList 在用户电影之间进行了非常简单的多对多设置。这是一个在 iTunes api 中搜索电影的应用程序,在这里我添加了用户将搜索到的电影保存到个人列表的功能。我正在使用 Rails 4.1.1 和 sqlite3。

class User < ActiveRecord::Base

  has_secure_password

  validates :email, presence: true, uniqueness: true
  validates :name, presence: true
  validates :password, length: { minimum: 6 }, allow_nil: true

  has_many :watch_lists
  has_many :movies, through: :watch_lists

  def add_movie(movie)
  self.movies << movie
end

class Movie < ActiveRecord::Base
  has_many :watch_lists
  has_many :users, through: :watch_lists
end

class WatchList < ActiveRecord::Base
  belongs_to :user
  belongs_to :movie
end

我的问题是当我尝试添加使用 iTunes id 作为 DB :id 的电影时。在第 7 行的 saves_controller 中,它为 nil:NilClass 抛出未定义的方法“名称”。我想不通,没有什么是零?电影保存到数据库很好,有正确的信息。这是我尝试创建连接表的时候。什么是rails试图调用'name'???

第 7 行引发错误 current_user.movi​​es undefined method 'name for nil:NilClass

class SavesController < ApplicationController
def create
  temp_movie = aquire_movie
  @movie = Movie.new(temp_movie.local_db_movie_info_hash)

  if @movie.save
    current_user.movies << @movie #### => LINE 7 ERROR undefined methond 'name for nil:NilClass
    redirect_to @movie
  end
end

private

def aquire_movie
  movie_data = itunes_results[:results][0]
  TemporaryMovie.new(movie_data)
end

def itunes_results
  data_retriever.get_data(params[:movie_id], search_by_id: true)
end

结束

这里也是架构。

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

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

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.string   "password_digest"
    t.string   "auth_token"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["auth_token"], name: "index_users_on_auth_token"
  add_index "users", ["email"], name: "index_users_on_email"

  create_table "watch_lists", force: true do |t|
    t.integer  "movie_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end

更新

我刚让一个朋友克隆了这个 repo,他没有任何问题,所以这是某种 gem /database 问题??

【问题讨论】:

  • 似乎是您的用户的问题,听起来绝对是数据问题。尝试从新开始这样做,看看会发生什么。删除整个数据库并从隐身浏览器登录。情况似乎是这样,因为它正在抱怨我只能将其视为您的用户的错误。

标签: ruby-on-rails ruby sqlite


【解决方案1】:

所以我通过这个找到了答案。 NoMethodError in CartsController#destroy - Agile Web Development with Rails 4

基本上更新了 rails 4.1.1 => 4.1.2,并且错误消失了。我相信活动记录中的某种错误就是它的原因。

当我运行 rspec 时,我也“收到警告:循环参数引用 - 反射”。这导致我到上面的帖子。

【讨论】:

  • 干得好!现在,您可以帮助其他读者选择此答案。
  • Rails 4.0.0 中也存在错误。我升级了一个项目,并且关联工作正常,没有对代码进行其他更改。
【解决方案2】:

我认为您不想在每次创建时都 raise 一个异常。

创建的标准模式是:

def create
  @movie = Movie.new(movie_params)
  if @movie.save
    redirect_to @movie
  else
    render 'new'
  end
end

当电影属于 current_user 时:

def create
  @movie = current_user.movies.new(movie_params)
  if @movie.save
    redirect_to @movie
  else
    render 'new'
  end
end

【讨论】:

  • 我认为你的第 7 行是对的,预计不会在这里加注。
  • 是的,但它的第 7 行抛出了 nil:NilClass 的未定义方法“名称”错误。我刚让一个朋友克隆了这个仓库,他没有任何问题,所以这是某种宝石/数据库问题??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
相关资源
最近更新 更多