I keep running into a recurring issue with my application. Basically, I have certain code that I want it to run when it first starts up the server to check whether certain things have been defined e.g. a schedule, particular columns in the database, existence of files, etc. and then act accordingly.
However, I definitely don't want this code to run when I'm starting a Rake task (or doing a 'generate', etc. For example, I don't want the database fields to be checked under Rake because the Rake task might be the migration to define the fields. Another example, I have a dynamic schedule for Resque but I don't want to load that when starting the Resque workers. And so on and so forth...
And I definitely need the Rake tasks to be loading the environment!
Is there any way of determining how the application has been loaded? I do want to run the code when its loaded via 'rails server', Apache/Passenger, console, etc. but not at other times.
If not, where or how could you define this code to ensure it is only executed in the manner described above?
The easiest way is checking some environment variable in your initialization code with something like
if ENV['need_complex_init']
do_complex_init
end
and running application with need_complex_init=1 rails s
I'm playing around with rails migrations and I've ran into a problem which I haven't been able to find a solution to.
Basically, I use the rails generators to create and destroy migrations to add and remove variables in models.
As a result of using 'rails destroy migration', my schema is all messed up. It doesn't reflect my present migrations at all. Is this a limitation of the destroy mechanism, or have I missed some crucial rake db: commands? Is there a way to reconstruct the schema from the current migrations?
I'm having trouble putting the pieces together, a link or keywords would suffice.
Rollback your migrations,
rake db:rollback to rollback to the last migration.
rake db:rollback STEP=3 will revert the last 3 migrations.
More about rake tasks here
In my rails app I have a model Events. It has several columns which were created through following this guide https://devcenter.heroku.com/articles/export-from-heroku-postgres and then performing rake db:schema:dump.
Now I want to add some new columns to the Event model. I tried editing schema.rb and restarting the app but that didn't seem to work. Anyone know the proper way to proceed?
----Edit-------
Specifically I added this line to the Event model in schema.rb.
t.datetime "date_time"
When I click the link to add a event in the rails app I receive this error:
undefined method `date_time' for #<Event:0x007fdb59a87118>
The schema.rb file is a representation of the current state of the database schema, it is written by the database migration process, not read.
If you want to add a new column, create a migration:
$ rails generate migration AddNewEventStuff
$ vim db/migrate/add_new_event_stuff*.rb
Then add your columns:
class AddNewEventStuff < ActiveRecord::Migration
def change
add_column ...
end
end
Everything except the add_column should be there already. Once you have your migration, do a rake db:migrate and you're done. Now you should see some changes in your schema.rb.
See the Ruby on Rails Migration Guide for further details and different ways to build your migrations.
You should try heroku run rake db:schema:load. That said, it's probably the wrong way to go about. Instead, you should make changes to your schema via migrations, and then run a heroku run rake db:migrate on it.
I have the following scenario:
I'm starting development of a long project (around 6 months) and I need to have some information on the database in order to test my features. The problem is that right now, I don't have the forms to insert this information (I will in the future) but I need the information loaded on the DB, what's the best way to handle this? Specially considering that once the app is complete, I won't need this process anymore.
As an example, lets say I have tasks that need to be categorized. I've begun working on the tasks, but I need to have some categories loaded on my db already.
I'm working with Rails 3.1 btw.
Thanks in advance!
Edit
About seeds:I've been told that seeds are not the way to go if your data may vary a bit, since you'd have to delete all information and reinsert it again. Say.. I want to change or add categories, then I'd have to edit the seeds.rb file, do my modifications and then delete and reload all data...., is there another way? Or are seeds the defenitely best way to solve this problem?
So it sounds like you'll possibly be adding, changing, or deleting data along the way that will be intermingled amongst other data. So seeds.rb is out. What you need to use are migrations. That way you can search for and identify the data you want to change through a sequential process, which migrations are exactly designed for. Otherwise I think your best bet is to change the data manually through the rails console.
EDIT: A good example would be as follows.
You're using Capistrano to handle your deployment. You want to add a new Category, Toys, to your system. In a migration file then you would add Category.create(:name => "Toys") or something similar in your migration function (I forget what they call it now in Rails 3.1, I know there's only a single method though), run rake db:migrate locally, test your changes, commit them, then if it's acceptable deploy it using cap:deploy and that will run the new migration against your production database, insert the new category, and make it available for use in the deployed application.
That example aside, it really depends on your workflow. If you think that adding new data via migrations won't hose your application, then go for it. I will say that DHH (David Heinemeier Hansson) is not a fan of it, as he uses it strictly for changing the structure of the database over time. If you didn't know DHH is the creator of Rails.
EDIT 2:
A thought I just had, which would let you skip the notion of using migrations if you weren't comfortable with it. You could 100% rely on your db/seeds.rb file. When you think of "seeds.rb" you think of creating information, but this doesn't necessarily have to be the case. Rather than just blindly creating data, you can check to see if the pertinent data already exists, and if it does then modify and save it, but if it doesn't exist then just create a new record plain and simple.
db/seeds.rb
toys = Category.find_by_name("Toys")
if toys then
toys.name = "More Toys"
toys.save
else
Category.create(:name => "More Toys")
end
Run rake db:seeds and that code will run. You just need to consistently update the seeds.rb file every time you change your data, so that 1) it's searching for the right data value and 2) it's updating the correct attributes.
In the end there's no right or wrong way to do this, it's just whatever works for you and your workflow.
The place to load development data is db/seeds.rb. Since you can write arbitrary Ruby code there, you can even load your dev data from external files, for instance.
there is a file called db/seeds.rb
you can instantiate records using it
user1=User.create(:email=>"user#test.com",
:first_name=>"user",
:last_name=>"name",
:bio=>"User bio...",
:website=>"http://www.website.com",
:occupation=>"WebDeveloper",
:password=>"changeme",
:password_confirmation=>"changeme",
:avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
)
user2=User.create(:email=>"user2#test.com",
:first_name=>"user2",
:last_name=>"name2",
:bio=>"User2 bio...",
:website=>"http://www.website.com",
:occupation=>"WebDeveloper",
:password=>"changeme",
:password_confirmation=>"changeme",
:avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
)
Just run rake db:seed from command line to get it into the db
I'm working on an application which uses data from db which has to be populated prior to the application being able to run. What I have to do is to populate few tables with few thousand rows but I'm not sure how I would do that in heroku because I have limited access to db for loading data.
What is the preferred way to do this?
Regards,
Johann
You can populate a Postgres database locally and then push it to heroku with heroku db:push see heroku help db:push
You may want to look into seeds, there's also a Railscast. I've never used this before, so you may alternately want to…
Create a rake task to suit your specific need. That way you can add the task to your Rails application and run heroku rake mytask. Here's a rake tutorial, and a Railscast on rake tasks to help get you started.