views:

81

answers:

2

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'},
+1  A: 

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"
Ryan Bigg
+2  A: 

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.

Nick Ragaz
That's certainly right, but what's unclear to me is what the OP is trying to do with `:value_id =>'3103'`
Isaac Cambron
Based on the OP's schema, I'm hoping and assuming that he has a County record with the id 3103. To make this work, the County (and other potential 'Values', e.g. City, Region) tables would have to be prepopulated with records with those ids. If that's not the case, then more code is needed (something like `:value => County.new([other county attributes here])` might work).
Nick Ragaz
Nick is correct. Sorry for the confusion with the naming. the `:value` is a population number. `:value_type` refers to the other model in the polymorphic relationship -- in this case, County. `:value_id` is the id number of the county (prepopulated).
Clay