Rake db:create was unknown in Rails 3.1.3

When I tried to perform rake db:create the system complained that it did not know how to build that tasks.

The fix was:

require ‘rails/all’ #Added this and commented out the lines below.
# require “action_controller/railtie”
# require “action_mailer/railtie”
# require “active_resource/railtie”
# require “sprockets/railtie”
#require “rails/test_unit/railtie”

And then in the Gemfile I commented out:

 

# Gems used only for assets and not required
# in production environments by default.
# group :assets do
# gem ‘sass-rails’, ‘~> 3.1.5’
# gem ‘coffee-rails’, ‘~> 3.1.1’
# gem ‘uglifier’, ‘>= 1.0.3’
# end

 

Tagged

ActionController Filters

Filter chain is halted if the action renders or redirects.

try me

Handy try method can help silence the NoMethod Error that is thrown if call a method on a nil object.

Ex.

user = User.find_by_name “dudeIdontexist”
user.email #=> bound to throw a nasty error, which is a good thing if you believe in the fail fast and loudly philosophy.

OR

user = User.find_by_name “dudeIdontexist”
user.try(:email) #=> nil. Now you can put it in if statements and it will handle the test without throwing an exception.

find_or…

create = Finds or creates the objects (new + save!)

initialize = Finds or returns a new record (without saving)

Ruby Handy Techniques for dealing with similar methods

Ex you have
# Without 
define_method:
def user; @data[:user]; end
def email; @data[:email]; end
def food; @data[:food]; end< /code>

# With define_method:
%w(user email food).each do |meth|
define_method(meth) { @data[meth.to_sym] }
end

http://www.trottercashion.com/2011/02/08/rubys-define_method-method_missing-and-instance_eval.html

Adding a hidden value param

hidden_field(:object, :field, :value => params[:requestval])

Thanks
http://apidock.com/rails/ActionView/Helpers/FormHelper/hidden_field

Rails Render Status Symbols

100 = :continue

101 = :switching_protocols

102 = :processing

200 = :ok

201 = :created

202 = :accepted

203 = :non_authoritative_information

204 = :no_content

205 = :reset_content

206 = :partial_content

207 = :multi_status

226 = :im_used

300 = :multiple_choices

301 = :moved_permanently

302 = :found

303 = :see_other

304 = :not_modified

305 = :use_proxy

307 = :temporary_redirect

400 = :bad_request

401 = :unauthorized

402 = :payment_required

403 = :forbidden

404 = :not_found

405 = :method_not_allowed

406 = :not_acceptable

407 = :proxy_authentication_required

408 = :request_timeout

409 = :conflict

410 = :gone

411 = :length_required

412 = :precondition_failed

413 = :request_entity_too_large

414 = :request_uri_too_long

415 = :unsupported_media_type

416 = :requested_range_not_satisfiable

417 = :expectation_failed

422 = :unprocessable_entity

423 = :locked

424 = :failed_dependency

426 = :upgrade_required

500 = :internal_server_error

501 = :not_implemented

502 = :bad_gateway

503 = :service_unavailable

504 = :gateway_timeout

505 = :http_version_not_supported

507 = :insufficient_storage

510 = :not_extended

as_json Only when you json

Use as_json to format data that have to be rendered in a json format.  The formatting will be specific to json.

 

Why am I passing options={} everytime I call as_jon.   This is so that options being passed to to_json are honored:

 

For example:

{:user.as_json(:only => [:email] , :methods => [:getName])}

You can add data to the json object that this way:

def as_json
  method_name #<< Will be called and results will be added to the called json.
end

Array of hashs

This is how to add Array of hashes to an existing Hash

es.map do |e|

e.attributes.merge(
:bowls => bowls.map { |eq| Hash[:bowls, eq.attributes] },
:spoon => spoons.map { |m| Hash[:spoons, m.attributes] },
:knives => knives.map { |c| Hash[:knives, c.attributes] }
)

end

Interesting Trick.  Start with Array and convert to hash and back

irb(main):054:0> array
=> [:peanut, :butter, :and, :jelly]
irb(main):055:0> Hash[*array]
=> {:and=>:jelly, :peanut=>:butter}
irb(main):057:0> h = Hash[*array]
=> {:and=>:jelly, :peanut=>:butter}
irb(main):058:0> h
=> {:and=>:jelly, :peanut=>:butter}
irb(main):059:0> h.to_a
=> [[:and, :jelly], [:peanut, :butter]]
irb(main):060:0> h.to_a.flatten
=> [:and, :jelly, :peanut, :butter]
irb(main):061:0> Hash[*h.to_a.flatten]
=> {:and=>:jelly, :peanut=>:butter}

Array Map — Killing Nils

jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}    
 => [3, 3, 3, nil, nil, nil] 


jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}.compact
 => [3, 3, 3]