Ruby on Rails:如何在多态模型中播种 *_type 列?

发布于 2024-09-09 17:59:48 字数 1194 浏览 7 评论 0原文

我有很多数据正在尝试将其植入 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 =>县值导致“String:Class 的未定义方法‘base_class’”。

我有数万个这样的值,我想将它们植入数据库中。它们与上面的值类似,只是有些与 County 模型相关,有些与 State 模型相关,有些与 City 模型相关。它们是静态值,在植入数据库后不会被编辑。

如何将模型播种到 :value_type 字段中?

(或者......我是否错误地处理了这个问题,如果是这样,你会如何处理它?)

编辑:schema.rb 文件的相关部分:

Isaac -

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'},

I have a lot of data that I'm trying to seed into a polymorphic model in Rails 2.3.8. The association for all of the data is with the County model. The data looks like:

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'},
  ...
])

The :value_type => County values lead to "undefined method `base_class' for String:Class."

I have tens of thousands of these values that I would like to seed into the database. They are similar to the values above except some are associated with the County model, some with the State model, and some with the City model. They are static values that will not be edited after seeding into the database.

How do I seed the model into the :value_type field?

(or... am I approaching this incorrectly and if so, how would you approach it?)

Edit: The relevant part of the schema.rb file:

Isaac -

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

I tried the following on the seeding, too, and it didn't work (County in quotes):

{ :value => '14389', :value_type => 'County', :value_id =>'3103'},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵山崖 2024-09-16 17:59:48

您绝对不需要架构中的“value”列——只需要“value_id”和“value_type”。那么您的种子数据应如下所示:

...
{ :value_id => 12345, :value_type => "County" },
...

请注意,“County”是引号中的字符串。

另一种选择是这样做:

{ :value => County.find(12345) }

然后 Rails 会根据 County 记录的类名和 id 自动设置 :value_type:value_id 列。这个例子可能会让您更好地了解正在发生的事情。但是,对于数千条记录,这会慢得多,因此第一种方法可能更适合这种情况。

You definitely don't need the "value" column in your schema -- just "value_id" and "value_type". Then your seed data should look like this:

...
{ :value_id => 12345, :value_type => "County" },
...

Note that "County" is a string in quotes.

Another alternative would be to do this:

{ :value => County.find(12345) }

And then Rails will automatically set the :value_type and :value_id columns for you based on the class name and id of the County record. This example might give you a better idea of what's going on. However, for thousands of records this would be much slower, so the first approach is probably better for this case.

丑疤怪 2024-09-16 17:59:48

发生这种情况是因为您已经在模型中执行了此操作:

belongs_to :value, :polymorphic => true

并且因为您也尝试在表上设置值列。 Rails 将无法区分您通过此方法设置关联或列之间的区别。要设置列,请使用以下命令:

self[:value] = "something"

This is happening because you've done this in your model:

belongs_to :value, :polymorphic => true

And because you're trying to set the value column on the table too. Rails will not be able to tell the difference between you setting the association or the column via this method. To set the column use this:

self[:value] = "something"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文