New to the rails world. Using 4.2.4.
I'm trying to create a model for my app and write some unit tests for it, but I'm running into difficulty after running a change migration. I created a model with
rails generate model player first_name:string last_name:string dispaly_name:string
and ran rake db:migrate RAILS_ENV=test
Then I wrote a (failing) unit test to make sure the proper fields were set when calling save. At this point, I realized that I misspelled dispaly_name, so I created a change migration that fixed the column name.
Now when I try to run the unit test rake test test/models/player_test.rb, I get an error (UndefinedColumn) that says the save failed because the Player model is still trying to save with the dispaly_name instead of display_name. I ran rake db:migrate rake db:migrate RAILS_ENV=test rake db:test:prepare and ran rails c and RAILS_ENV=test rails c and checked that the column name was set properly by running Player.column_names. I also checked schema.rb. Everything seems to be in order, but I can't figure out why the Player.new in my test case is using the old column name.
Can someone explain what's happening?
I got it:
when you create model then a fixture along with test file is created.
you created your model with field name dispaly_name, and in file YourApp/test/fixtures/player_test.yml a field dispaly_name is created. right?.
but the problem is that when you change field name to display_name it changes in MODEL and TABLE too but not in fixture file. so you need to correct your field in fixture file too.
please change field name dispaly_name in file YourApp/test/fixtures/player_test.yml
I hope it will work :)
Depending upon what you are using to populate your test db, FactoryGirl, Fabrication or the default Fixtures a Rails application come up with, change the data field name there. Then the error will disappear.
Do try restarting the server just in case if the error persists. I dont think so restarting the server will have any effect but just for a try.
Try to run bundle exec rake:test:clone, this recreates test db schema from current development one.
Related
I created a model with a column named errors and when I ran the test I naturally got an ActiveRecord::DangerousAttributeError
so I rolled back the migration and changed the column name to error_messages and reran the migration. I don't refer to that column yet in any other part of my code.
Now I can create valid objects from this model in the rails console, but still the test is giving the same error. How do I make this error go away?
I needed to refresh the database structure in the testing environment like this:
bundle exec rake db:reset RAILS_ENV=test
I'm trying to generate a Post model in rails using the following code:
rails g model Post url:string, title: string
When I execute that line I get the following output:
invoke active_record
Another migration is already named create_posts: /Users/myname/Desktop/folder/my_project/db/migrate/20121212021831_create_posts.rb
It seems to be expressing a conflict as though the file already exists in my Model folder - which it does not.
Is this a naming problem? Any thoughts?
The conflicting migration would be in your db/migrate folder, not app/models.
Your two options are naming your new migration something else or removing the old migration. If you choose to remove the old migration, make sure to roll it back first before deleting it, so that your database schema is correct.
This problem can happen when you execute rails g model post for multiple times. You can resolve this issue by executing the rails destroy model post to remove last generated content.
When I ran bundle exec rake db:test:prepare I got the following:
rake aborted!
Multiple migrations have the name CreateMicroposts
To check the status of my migration files, I ran
rake db:migrate:status
And got:
Status Migration ID Migration Name
------- --------------- -----------------
up 20120616205407 Create users
up 20120622103932 Add index to users email
up 20120622114559 Add password digest to users
up 20120628095820 Add remember token to users
up 20120704123654 Add admin to users
down 20120706103254 Create microposts
up 20120707073410 Create microposts
As you can see, I have two migration files with the exact same names and the exact same code in them. It's only their statuses differ, i.e. Up and Down.
What does Up and Down signify?
And which one can I delete, if I have to?
The problem is that you have two different migration files containing the header
class CreateMicroposts< ActiveRecord::Migration
rake db:migrate:status does not check the status of your migration files. It tells you what migrations will be applied if you run rake db:migrate. The up/down labels are pretty much self-explanatory: it tells you whether the migration will be applied via the up method or the down method. The up method is ran when you migrate and the down when you rollback a migration. You can make some further reading about Rails migrations here.
up is the method called when "evolving" (ie migrating to a new schema), while down is the method called when "regressing" (ie migrating to an older schema version, because one of your changes doesn't suit you). db:migrate calls up, db:rollback calls down. In recent versions of rails, there's change that handles both at the same time.
As for the deletion... I don't do activerecord much these days, but I think you're free to do whatever you want with your files. I don't think deleting a duplicate file will do any harm, and if it does.. Well, you use source control, right ? :)
here's what happened. I used to have a Model called Message, I then wanted to rename it so I created a migration that renamed that table from Message to Thread. I had later migrations that then added to that table.
That worked fine, it terms of db:migrate to move forward with our existing databases. Now I noticed that when I do a db:create to create a fresh db it fails, as rails creates Message, then when it goes to add a field to message I get a:
uninitialized constant AddActiveMessageIdToWalls::Message
I think the problem is that I also renamed all the controller & models from message to thread and now the migration can't find the model when migrating? Does that sound right?
How do you deal with this in the rails world? Thanks
I like to keep my db/schema.rb updated to avoid this. So when I need to create the db in a new environment, I just need to do the rake db:create and rake db:schema:load. No migration is needed.
easiest solution: just add an empty Message < ActiveRecord::Base subclass in the earlier migration.
... migration file ...
class Message < ActiveRecord::Base;end
this is a very silly question I think: I just removed a table named person_emails I created a minute ago in a new demo app I created half an hour ago. Then I started testing like just now, when I ran a unit test on an unrelated model called line_item, and I got "ActiveRecord::StatementInvalid: Mysql::Error: Table 'depot_test.person_emails' doesn't exist: DELETE FROM person_emails"
I did do the rake db:test:prepare and rake db:migrate RAILS_ENV=test.
Also, I had this column named "price" in the line_items table which I used a migration to remove, but the test tests for it anyway and throws and error. Is there something that I always should do for tests after reverting a migration or using a migration to remove a column?
Any ideas?
Thank You!
I'm pretty sure this is happening because you still have a person_emails fixture file! It's trying to clear the table before it loads the fixture data. Check for a file called test/fixtures/person_emails.yml and delete it, and you should be set.