Category Archives: Rails

google api client no such file to load

Getting the following error when running annotate for my models

…/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require’: No such file to load — google/api_client (LoadError)

Try downgrading your google-api-client to a version below 0.9, namely…

Modify your Gemfile to have this line instead of what is there now for the google api gem

gem ‘google-api-client’, ‘<0.9’

rails console hangs

“rails c” hangs with no error messages and no response.

solution: stop spring using the following command
“spring stop” or “bin/spring stop”

spring will automatically start back up when you run the rails c command.

upgrade ruby in rvm without losing gems

How can I upgrade the ruby interpreter without losing my gems inside my gemsets?

Use rvm upgrade

Say I want to upgrade from ruby 1.9.3 to ruby 1.9.3-p286 from within rvm and not loose my gemset environments.  I would type the following command.

rvm upgrade 1.9.3 1.9.3-p286

Then you just follow the command line prompts that come up, simple as that. =D

index name too long in rails 3.2 and mysql 5.5

If you get an error that says the auto-generated index name is too long (limited to 64 characters) during a rails migration, add a name to it in the last argument to overwrite the generated name with your custom shorter name.

Sample Error Message:

== AddColumnNameToPrices: reverting ============================================
— remove_column(:prices, :column_name)
rake aborted!
An error has occurred, this and all later migrations canceled:

Index name ‘temp_index_altered_prices_on_column_and_other_column_and_third_column’ on table ‘altered_prices’ is too long; the limit is 64 characters

Solution:
add_index :designations, [ :scope_type, :scope_id, :role_id, :user_id ], :unique => true, :name => ‘my_index’

how generate a scaffold on already created model?

If you have already created data models in your rails app but wish to scaffold or stub out the controller and the views without starting over the models, you can use the option scaffold_controller

rails generate scaffold_controller <name_of_controller_or_model_pluralized>

For a list of possible generation options, use the -h flag for help.

rails generate -h

String to Boolean conversion

Ever wonder how to convert  the String values of “true” or “false” to the boolean value true false in rails? Rails, specifically ActiveRecord::ConnectionAdapters::Column has a built in function which does just this called value_to_boolean.  

Example(s):

"true".value_to_boolean

params[:is_string_true_false].value_to_boolean

will return the boolean value true.

 

For plain old Ruby, this is not so straightforward, but simple enough.  Since you can very easily override Ruby core classes, we’re going to override the Ruby String class and add a to_bool function which converts a String value to Boolean.

Now we can call on this method for any string. “True”.to_bool ==> true. If you pay attention to the code, you’ll notice that “1” “yes” “y” will be converted to true, and “f”, “no”, “n”, “0”, will convert to false. This works the same for value_to_boolean in rails. This is particularly helpful when getting data from form inputs.

 

* Caution!: One thing to note for the to_bool implementation. “”.to_bool will return false. (that is quotes with no spaces in between).