Seed data

Hi
I am using seed_fu to seed initial data to tables
(GitHub - mbleigh/seed-fu: Advanced seed data handling for Rails, combining the best practices of several methods together.) . 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)

And now when I do select * from states; I get unexpected result as

±----±-----±----------------------±-----±-----------+
| id | abbr | name | fips | country_id |
±----±-----±----------------------±-----±-----------+
| 5 | AL | Alabama | 01 | 1 |

   What I expect was  id=1 But I got id as 5 . Why this ?

Thanks in advance
Tom

On 26 March 2010 10:19, Tom M. [email protected] wrote:

}
But now I modified state.rb as

And now when I do select * from states; I get unexpected result as

±----±-----±----------------------±-----±-----------+
| id | abbr | name | fips | country_id |
±----±-----±----------------------±-----±-----------+
| 5 | AL | Alabama | 01 | 1 |

  What I expect was  id=1 But I got id as 5 . Why this ?

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

On 26 March 2010 13:16, tom [email protected] wrote:

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

On Fri, Mar 26, 2010 at 3:19 AM, Tom M. [email protected] wrote:

}
But now I modified state.rb as

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

On 29 March 2010 05:42, Tom M. [email protected] wrote:

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?

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