【发布时间】:2010-07-13 23:30:23
【问题描述】:
我有很多数据正在尝试植入 Rails 2.3.8 中的多态模型中。所有数据的关联都与县模型有关。数据如下:
data = Datum.create([
...
{ :value => '14389', :value_type => County, :value_id =>'3103'},
{ :value => '59013', :value_type => County, :value_id =>'3105'},
{ :value => '17117', :value_type => County, :value_id =>'3106'},
...
])
:value_type => County 值导致“String:Class 的未定义方法 `base_class'”。
我有数以万计的这些值,我想将这些值植入数据库。它们与上面的值类似,除了一些与县模型相关,一些与州模型相关,还有一些与城市模型相关。它们是静态值,在播种到数据库后不会被编辑。
如何将模型植入 :value_type 字段?
(或者......我是否错误地处理了这个问题,如果是,你会如何处理它?)
编辑:schema.rb 文件的相关部分:
艾萨克-
create_table "data", :force => true do |t|
t.integer "value"
t.string "value_type"
t.integer "value_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "counties", :force => true do |t|
t.string "name"
t.integer "state_id"
t.integer "ansi_code"
t.string "ansi_class"
t.datetime "created_at"
t.datetime "updated_at"
end
我在播种时也尝试了以下方法,但没有成功(引号中的县):
{ :value => '14389', :value_type => 'County', :value_id =>'3103'},
【问题讨论】:
-
你能发布你的schema.rb的相关部分吗?
-
拥有
value、value_type和value_id没有意义。 Datum 模型将使用value_type和value_id在您使用somedatum.value之类的内容访问另一个表时在另一个表中找到正确的记录 -
Jamie - 我按照第 1 页上的 Obie“The Rails Way”示例进行操作。 215 其中,在 Comment 模型中有
subject、subject_id和subject_type字段。 Comment 模型包括belongs_to :subject, :polymorphic => true,然后其他类有has_many :comments, :as => :subject。我的基准模型包括belongs_to :value, :polymorphic => true行。我的县模型包括行has_many :data, :as => :value。 -
杰米说得对。我没看过那本书,但我不认为你的解释是正确的。另一种让我们困惑的方式是:你的种子脚本中的 14389 是什么?比如,这个数字是什么意思?如果是县 ID,3103 是什么?
-
你真的应该给你的模型起比 Datum 更多信息的名字。一切都是数据。
标签: ruby-on-rails ruby seed polymorphism