Adding sampled data to the database using a rail for a rail motor

I'm trying to get Rails engines up and running by creating an ad engine where users can view / post / respond to ads.

The main app contains the code to authenticate users and profiles, while there is an engine I created that will work with the ad functionality.

Now I want to add some sample data to the database for the ad engine. So I created a rake file named "sample_classifieds_data.rake" under "vendor / plugins / classifieds / lib / tasks" and I added yml files under "vendor / plugins / classifieds / lib / tasks / sample_classifieds_data"

The rake file code and sample yml file can be found here: http://gist.github.com/216776

Now the problem is that when I run the rake task, no error occurs, but the values ​​are not populated in the database.

Any ideas? BTW, this is a development environment and a database is a development database.

I did a similar rake task to populate an example of users in the database that worked. the location of this rake file 'sample_data.rake' was in 'lib / tasks'.

+2


source to share


3 answers


Your task looks good. The only thing that might be causing your task to fail is that the file you pass to Fixture.new does not point to a yml or csv file.

Double check by changing the put statement to print the full path of the imported file and compare what it prints against your directory structure.



For example, is something going wrong if your file files start with a capital letter? .Yml categories instead of .yml categories

+1


source


In the rails area, you can use a function rake db:seed

to add data to your base. See commit .

The usage is pretty simple.

Create a file db/seeds.rb

.
And put whatever code you want to seed in it.

For example:



Category.create!(:name => 'My Category')
Country.create!(:name => 'Cassoulet Land')

      

And when you want to seed your database, you can do rake db:seed

If for some reason you don't want to use edge (which would be understandable in a production environment), you can use Seed Fu , which is quite a trick for you.

+9


source


The db: seed task was added in Rails 2.3.4. So no need to run.

http://weblog.rubyonrails.org/2009/9/4/ruby-on-rails-2-3-4

0


source







All Articles