铁轨上的红宝石。有许多通过关联

时间:2016-06-01 16:15:37

标签: ruby-on-rails ruby

我有一个类型和他们关联的电影模型有很多通过。从浏览器调用时我收到的关于分类的错误是连接模型。

错误:类的未定义局部变量或方法“分类”:0x00000007be0120>
这是控制器......

class GenreController < ApplicationController

   def updateList
     result=Net::HTTP.get(URI.parse('url'))
     json = JSON.parse(result)

     json['genres'].each do |data|
       obj = Genre.new(
            tmdb_id: data['id'],
            name: data['name']
            )
     end

   end

end

这是模型

class Genre < ActiveRecord::Base

  has_many :categorizations
  has_many :movies, :through => categorizations

end

我知道应该有这方面的验证,但现在我只是填充流派表。应该有一个创造,但我只是想看到结果。

修改

分类模型

class Categorization < ActiveRecord::Base

  belongs_to :movie
  belongs_to :genre
end

电影模型

  class Movie < ActiveRecord::Base

      has_many :roles
      has_many :actors, :through => :roles

      has_many :watchlists
      has_many :users, :through => :watchlists

      has_many :categorizations
      has_many :genres, :through => :categorizations

      has_many :videos

  end

1 个答案:

答案 0 :(得分:4)

has_many :movies, :through => categorizations

您需要在&#34;分类&#34;:

之前使用冒号
has_many :movies, :through => :categorizations