1

我有以下迁移和模型:

class CreatePlatforms < ActiveRecord::Migration
  def change
    create_table :platforms do |t|

        t.integer :user_id
        t.string :name
        t.string :platform_id
        t.timestamps
    end
  end
end

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|

        t.string :email
        t.string :first_name
        t.string :last_name
        t.string :gender
        t.date :birthday
        t.timestamps
    end
  end
end

class Platform < ActiveRecord::Base
    belongs_to :user
end

class User < ActiveRecord::Base
    attr_accessible :email, :first_name, :last_name, :gender, :birthday
    has_many :platforms
end

有了这个定义,我可以创建用户和平台:

user = User.new
platform1 = Platform.new
platform2 = Platform.new

甚至我可以将用户与平台相关联:

platform1.user = user

但是,当我尝试将平台与用户关联或从用户那里获取平台时,它会崩溃:

user.platforms << user or user.platforms
NoMethodError: undefined method `platforms' for #<User:0x007f8cfd47a770>
4

1 回答 1

3

事实证明,问题出在数据库字段platform_id上。这弄乱了轨道。只需将其删除即可。

于 2013-11-02T11:18:01.430 回答