Create and run separated seed files

Usually We use the great power of rails db:seed in any Rails project. The seeds file provides us a way to quickly manipulate our sample and necessary data, especially at the beginning of our project.

But when times gone, we see that we often add more and more data to our seeds file and we need to rerun the command again. So, we need to comment out the old code in seeds file, or, we need to strictly validate the models. We absolutely don’t want our seed data as a mess

That’s why I start investigating the way to separate our seeds into different files, so we can choose whichever seed file we want to run, to void duplication or commenting old code. But we need a way for other programmer who joins our project later to run all of the files.

Let’s move on.

Continue reading Create and run separated seed files

Add parameters to your Rails tasks

I think adding parameters to Rake task is usually necessary. What is rake? It’s a ruby utility that you can use to build up a list of tasks, for your project, in a dynamic way. In order to create a rake task, you can just run

rails g task your_tasks_domain your_first_task your_second_task

Or, you can, create in a manual way, by adding a .rake file in lib/tasks folder

# lib/tasks/your_task_domain.rake
namespace :your_tasks_domain do
  desc "TODO"
  task your_first_task: :environment do
  	# You will write your code here
  	# For example: Task.create(name: "my first task")
  end
  desc "TODO"
  task your_second_task: :environment do
  end
end

Continue reading Add parameters to your Rails tasks