Rails中的关联问题

时间:2011-02-08 14:14:04

标签: ruby-on-rails ruby-on-rails-3

我一直在研究我的Rails应用程序而且我遇到了一些我无法理解的关联。

以下是我的模特:

用户模型

class User < ActiveRecord::Base
   has_many :events, :dependent => :destroy
end

活动模型

class Event < ActiveRecord::Base
  belongs_to :user
  has_many :items, :dependent => :destroy
end

项目模型

class Item < ActiveRecord::Base
   belongs_to :event
end

当我进入rails控制台并执行以下操作时:

> User.last.events.last.items

我收到这样的错误:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: items.event_id: SELECT "items".* FROM "items" WHERE ("items".event_id = 1)

我已经设置了这样的迁移:

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.string :description
      t.string :name
      t.integer :event_id

      t.timestamps
    end
    add_index :items, :event_id
  end

  def self.down
    drop_table :items
  end
end

这与我的协会和我的方式有关吗? 或者是协会的深度?

我希望我提供了足够的信息。

谢谢大家!

1 个答案:

答案 0 :(得分:0)

我建议你仔细检查一下你是否已经运行了迁移(rake db:migrate)。

没关系,但你也可以尝试“t.references:event”而不是“t.integer:event_id”。

相关问题