underscore method
Rails' ActiveSupport adds underscore to the String methods using the following code (This is NOT a String method in Ruby)
class String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
Makes an underscored, lowercase form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
'ActiveModel'. underscore # => "active_model"
'ActiveModel::Errors'.underscore # => "active_model/errors"
'ActiveModel::Errors'.underscore # => "active_model/errors"
As a rule of thumb you can think of it as the inverse of camelize, though there are cases where that does not hold:
'SSLError'
CREATE & DESTROY
$ rails generate controller FooBars baz quux
$ rails destroy controller FooBars baz quux
$ rails generate model Foo bar:string baz:integer
$ rails destroy model Foo
$ rake db:migrate
We can undo a single migration step using
$ rake db:rollback
To go all the way back to the beginning, we can use
$ rake db:migrate VERSION=0
No comments:
Post a Comment