Ruby on rails 4 属于不在rails 4.0中工作

Ruby on rails 4 属于不在rails 4.0中工作,ruby-on-rails-4,belongs-to,Ruby On Rails 4,Belongs To,我正在用rails 4.0构建一个小示例。当我在两个模型之间创建一个一对多关系时,有些奇怪。一个集群有多个:请求,一个请求属于:集群 class Cluster < ActiveRecord::Base has_many :requests paginates_per 10 end class Request < ActiveRecord::Base paginates_per 10 belongs_to :cluster, :class_name

我正在用rails 4.0构建一个小示例。当我在两个模型之间创建一个一对多关系时,有些奇怪。一个集群有多个:请求,一个请求属于:集群

class Cluster < ActiveRecord::Base
    has_many :requests
    paginates_per 10
end

class Request < ActiveRecord::Base
    paginates_per 10
    belongs_to :cluster, :class_name => "Cluster"
end
类集群“集群”
结束
然后,当我创建一个集群并插入一些请求时,我得到一个错误

def do_cluster
    requests = Request.where(:state => 'new').limit(4)
    cluster = Cluster.new()
    cluster.state = "new"
    requests.each do |request|
      cluster.requests << request
      puts "abc"
      request.state = "clustered"
    end
    cluster.save
    redirect_to "index"
  end
def do_集群
请求=请求。其中(:state=>new')。限制(4)
cluster=cluster.new()
cluster.state=“新建”
请求。每个do |请求|
cluster.requests在16毫秒内完成了500个内部服务器错误(ActiveRecord:1.0毫秒)

ActiveModel::MissingAttributeError(无法写入未知属性
cluster\u id
): app/controllers/admin_controller.rb:21:in'do_cluster'


我不明白我做错了什么。有人能帮忙吗?

您需要知道的问题是,尽管
ActiveRecord
和数据库上的表是通过Rails中的约定连接的,但这并不意味着模型中的
关联将在数据库中创建链接(外键,甚至列)

在您的情况下,在创建表
'requests'
时,需要在表
'requests'中有一个名为
cluster\u id
的列

型号

class Cluster < ActiveRecord::Base
    has_many :requests
    paginates_per 10
end

class Request < ActiveRecord::Base
    paginates_per 10
    belongs_to :cluster
end
类集群
迁移

class CreateRequests < ActiveRecord::Migration
  def change
    create_table :requests do |t|
      # any fields
      t.integer :cluster_id

      t.timestamps
    end
  end
end
class CreateRequests
您需要知道的问题是,尽管
ActiveRecord
和数据库上的表是通过Rails中的约定连接的,但这并不意味着模型中的
关联将在数据库中创建链接(外键,甚至列)

在您的情况下,在创建表
'requests'
时,需要在表
'requests'中有一个名为
cluster\u id
的列

型号

class Cluster < ActiveRecord::Base
    has_many :requests
    paginates_per 10
end

class Request < ActiveRecord::Base
    paginates_per 10
    belongs_to :cluster
end
类集群
迁移

class CreateRequests < ActiveRecord::Migration
  def change
    create_table :requests do |t|
      # any fields
      t.integer :cluster_id

      t.timestamps
    end
  end
end
class CreateRequests
您需要知道的问题是,尽管
ActiveRecord
和数据库上的表是通过Rails中的约定连接的,但这并不意味着模型中的
关联将在数据库中创建链接(外键,甚至列)

在您的情况下,在创建表
'requests'
时,需要在表
'requests'中有一个名为
cluster\u id
的列

型号

class Cluster < ActiveRecord::Base
    has_many :requests
    paginates_per 10
end

class Request < ActiveRecord::Base
    paginates_per 10
    belongs_to :cluster
end
类集群
迁移

class CreateRequests < ActiveRecord::Migration
  def change
    create_table :requests do |t|
      # any fields
      t.integer :cluster_id

      t.timestamps
    end
  end
end
class CreateRequests
您需要知道的问题是,尽管
ActiveRecord
和数据库上的表是通过Rails中的约定连接的,但这并不意味着模型中的
关联将在数据库中创建链接(外键,甚至列)

在您的情况下,在创建表
'requests'
时,需要在表
'requests'中有一个名为
cluster\u id
的列

型号

class Cluster < ActiveRecord::Base
    has_many :requests
    paginates_per 10
end

class Request < ActiveRecord::Base
    paginates_per 10
    belongs_to :cluster
end
类集群
迁移

class CreateRequests < ActiveRecord::Migration
  def change
    create_table :requests do |t|
      # any fields
      t.integer :cluster_id

      t.timestamps
    end
  end
end
class CreateRequests
在您的
请求
表中是否有
cluster\u id
吗?不,我已在我的schema.rb中进行了检查,它没有出现。您的
请求
表中是否有
cluster\u id
吗?不,我已在我的schema.rb中进行了检查。它没有出现。您的
请求
表中是否有
cluster\u id
没有,我已签入我的schema.rb,它没有出现。您的
请求
表中是否有
cluster\u id
?不,我已签入我的schema.rb,它没有出现。