seed data

Hi
   I am using seed_fu to seed initial data to tables
(http://github.com/mbleigh/seed-fu) . And in db/fixtures/state.rb

I have values like (I am filling only two data for simplicity)

State.seed_many(:name, :abbr,:fips,:country_id, [
      { :name => "Alberta", :abbr => 'AB',:fips => '01', :country_id =>
2 },
      { :name => "Alaska", :abbr => 'AK',:fips => '02', :country_id => 1
}
])

It works and when I do select * from states; I get result as

I _think_, though I may be corrected, that when you do create it
allows the database to set the id. If you provide one yourself it is
ignored. The database has allocated the next in sequence even though
earlier ones have been removed.

Colin

hi, can u fill the database with the current_user?

i want / need to fill a few tables after the user got activated, but the tables i need to fill have to have the user_id in it…is that possible?

thx

hi, can u fill the database with the current_user?

i want / need to fill a few tables after the user got activated, but the
tables i need to fill have to have the user_id in it...is that possible?

I don't understand what you mean. Setting a field user_id is not a
problem, it is just the primary key id field that is a problem.

Colin

Hi
   So my question is how can I ensure that even after running rake
db:seed (rake db:seed_fu) a number of times my primary key ie, id here,
does not get modified? Should I need to write an update_id function
again?

Thanks
Tom

Hi

I am using seed_fu to seed initial data to tables

(http://github.com/mbleigh/seed-fu) . And in db/fixtures/state.rb

I have values like (I am filling only two data for simplicity)

State.seed_many(:name, :abbr,:fips,:country_id, [

  { :name => "Alberta", :abbr => 'AB',:fips => '01', :country_id =>

2 },

  { :name => "Alaska", :abbr => 'AK',:fips => '02', :country_id => 1

}

])

It works and when I do select * from states; I get result as

±----±-----±----------------------±-----±-----------+

id | abbr | name | fips | country_id |

±----±-----±----------------------±-----±-----------+

1 | AB | Alberta | 01 | 2 |

2 | AK | Alaska | 02 | 1 |

But now I modified state.rb as

State.seed_many(:name, :abbr,:fips,:country_id, [

  { :name => "Alberta", :abbr => 'AB',:fips => '01', :country_id =>

2 },

  { :name => "Alaska", :abbr => 'AK',:fips => '02', :country_id => 1

}

])

State.destroy_all

State.create(:id => 1,:name => “Alabama”, :abbr => ‘AL’,:fips => ‘01’,

:country_id => 1)

Tom, you shouldn’t be setting the value of the :id field because the database

will ignore your setting and assign the next unique identifier automatically.

Could you explain what you’re trying to do in a step by step scenario? This

may helper others on the mailing list to better assist you.

-Conrad

If you really need the id to be pre-defined (though why you need to do
this I do not know, it is generally a bad idea) then in your seed code
you could check whether a record of that id already exists and if it
does then update it rather than re-creating it.

Colin

Hi
  Thanks for your reply

Could you explain what you're trying to do in a step by step scenario?
This
may helper others on the mailing list to better assist you.

   What I tried is as my first post. Now just as a test a I did like

State.destroy_all
State.create(:id => 1,:name => "Alabama", :abbr => 'AL',:fips => '01',
:country_id => 1)

   But after this step whenever I do rake db:seed_fu

I dont get what I expected for the id fields. I expect id to be
generated like
1,2,3....

If you really need the id to be pre-defined (though why you need to do
this I do not know, it is generally a bad idea)

      Why I need the id to be predefined means suppose at a later stage
if already that id may e saved to a number of tables as foreign key. But
suppose if execute rake db:seed_fu then if the id changed to 5,6,7
instead of 1,2,3
the total solution may be a big fail
        So when I checked the way which I mentioned in my first post
accidently I noticed this. Then post a message to a get a clarification
since I dont know where the fault is

         I think one more thing to ensure the ids are the values that I
expected is, after creation call a function like

      @country.update_id_to(1) .....

Thanks
Tom