running test coverage (rcov) and displaying results in one hit
Reminder re how to run “rcov” for code coverage, and display the results in a browser in one hit.
Solution:
rake spec:rcov && open coverage/index.html
Reminder re how to run “rcov” for code coverage, and display the results in a browser in one hit.
Solution:
rake spec:rcov && open coverage/index.html
Had an issue trying to resolve the following error I was getting in one of my test specs (i.e. RSpec tests) recently:
undefined method `account_item_id=' for #
When I did the same thing the test spec was doing in the rails console (i.e. ./script/console) things worked fine. Couldn’t work it out.
Solution: You may have guessed it. The tests work on the “test” database, not the “development” database. I needed to run the “rake db:migrate RAILS_ENV=test” to bring my test database schema up to date. Doh!
Not being an SVN (subversion) guru I wanted to find a way to recursively add files (svn add) to put them under version control.
Solution: Here’s the solution (updated from Paul’s suggestion below):
“svn add * –force” This will recursively add new files and skip any files that are already in svn.
The original solution I was using was the below:
svn status | grep "^\?" | awk '{print $2}' | xargs svn add
I recently was stuck trying to work out why a “save!” on one of my models was not working. I had checked and verified that the model object was valid first. I checked the database directly and there were definitely no record being saved.
Console Output Highlight the Issue
>> a = Allocation.new => #<Allocation id: nil, transaction_id: nil, person_id: nil, recurring_id: nil, amount: nil, amount_percent: nil, created_at: nil, updated_at: nil> >> ?> a.valid? => false >> a.amount = 1 => 1 >> a.transaction_id = 1 => 1 >> a.person_id = 1 => 1 >> ?> a.valid? => true >> ?> ?> a.save => nil >> a.save! => nil
SQL Commands Observed
Transaction Columns (0.003291) SHOW FIELDS FROM `transactions` Transaction Load (0.001494) SELECT * FROM `transactions` WHERE (`transactions`.`id` = 1)
Model Code
class Allocation < ActiveRecord::Base
belongs_to :person
belongs_to :transaction
validates_numericality_of :amount, :if => :amount
validates_numericality_of :amount_percent, :if => :amount_percent
private
def validate
errors.add_to_base('amount and amount_percent can not both be specified') if amount &amp;amp;amp;&amp;amp;amp; amount_percent
errors.add_to_base('either amount OR amount_percent must be specified') if !amount &amp;amp;amp;&amp;amp;amp; !amount_percent
end
end
Can you see the issue? (I couldn’t)
Solution:Transaction is a reserved class in Rails, and it was the name of one of my models, in this case it was one that my “Allocation” model had an association with. The “belongs_to transaction” line was creating a “transaction” method for reading the association. This overwrote an internal method called transaction. The internal method just runs its block inside a database transaction and is used on saves etc… By replacing that with a transaction method that does nothing with the block you completely neutre activerecord.
For a list of Rails reserved words see: http://wiki.rubyonrails.org/rails/pages/ReservedWords
I’ll be starting the renaming of the model “transaction” that I had shortly
Acknowledgements:Ryan Bigg, Frederick Cheung and Chris Bartlett from the Rails mailing list. Thanks guys.
Ever wanted to view the SQL statements being issued to your database when you are in the Rails console (i.e. ./script/console), i.e. in development mode. I posted on the mailing list and got one way forward here.
Solution: Run up a server in a separate terminal to view the SQL statements (i.e. ./script/server). They appear here fine.
I spent 15 minutes recently trying to work out why I was getting this error when I was trying to run a migration on my production site:
[root@home current]# rake db:migrate VERSION=production (in /u/apps/myequity/releases/20081206042650) rake aborted! No such file or directory - /tmp/mysql.sock (See full trace by running task with --trace)
Do you see the problem? What if I show you the db:migrate help…
[root@home current]# rake -D db:migrate
(in /u/apps/myequity/releases/20081206042650)
rake db:migrate
Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false.
So basically I was using VERSION rather than RAILS_ENV (which isn’t mentioned in the description)
Things worked fine after issuing the correct line:
[root@home current]# rake db:migrate RAILS_ENV=production (in /u/apps/myequity/releases/20081206042650) [root@home current]#