Camelcase Instead of Snake_Case in Rails Db

CamelCase instead of snake_case in Rails DB

@arkadiy,as a matter of fact, I was looking into this just this very day.

For table names, we of course have the set_table_name method:

class CamelCasedFoo < ActiveRecord::Base
set_table_name :CamelCasedTable
end

For things like primary keys, we have set_primary_key:

class CamelCasedBar < ActiveRecord::Base
...
set_primary_key "CamelCasedTableID"
end

And it should be possible to alias funky, legacy column names to something more rails-friendly with alias_attribute:

class CamelCasedBaz < ActiveRecord::Base
...
alias_attribute :preferred_delivery, :DeliveryFrequency
end

One key thing to keep in mind is to watch out for any column names that are ruby or rails keywords or magic field names.

Rails appears to have all that metaprogramming goodness to allow you to work around legacy db table names and columns. You may wish to have a read of Jonathan Hui's blog post on "Ruby on Rails 3 Model Working with Legacy Database". And you might want to have a look at the safe_attributes gem.

How to force Rails ActiveRecord to use CamelCase in queries, instead of snake_case

I don't think rails have any support as per your specification at base level. As rails believe in convention over configuration.

Although we can override the table name in your model.

user_table.rb

class UserTable < ApplicationRecord

  self.table_name = "UserTable"
self.primary_key = "YourPrimaryKey"

end

Why use Camel Case for JS and Snake Case for your DB?

For JavaScript it is up to you (or the guidelines of the project) if you would like to use camel case or not. But because almost every majaor library and the core API uses camel cases it is a good idea to do the same, because then you wont need to think about if you need to use the one or the other.

For DBMS it depends on the operating system and DBMS because you might get technical problems when using mixed letters. E.g. if you have a MySQL database running on a Windows-System, then MySQL would not care about the case of the table names, because the file system is case insensitive. As of that the default MySQL configuration on Windows will automatically convert all table names to lowercases. As long as your DBMS will stay on the windows machine this won't be a problem, but as soon as you decide to switch to a Linux base server then you will get huge problems to migrate your data from the windows to the linux box.

To be not dependent on those problems, it is common to only use lowercase letters with DBMS, no matter if the DBMS would have problems with uppercase letters or not. With lowercase letters you will definitely have no problems and with uppercase letters you need to do investigations if it can be a problem.

Converting nested hash keys from CamelCase to snake_case in Ruby

You need to treat Array and Hash separately. And, if you're in Rails, you can use underscore instead of your homebrew to_snake_case. First a little helper to reduce the noise:

def underscore_key(k)
k.to_s.underscore.to_sym
# Or, if you're not in Rails:
# to_snake_case(k.to_s).to_sym
end

If your Hashes will have keys that aren't Symbols or Strings then you can modify underscore_key appropriately.

If you have an Array, then you just want to recursively apply convert_hash_keys to each element of the Array; if you have a Hash, you want to fix the keys with underscore_key and apply convert_hash_keys to each of the values; if you have something else then you want to pass it through untouched:

def convert_hash_keys(value)
case value
when Array
value.map { |v| convert_hash_keys(v) }
# or `value.map(&method(:convert_hash_keys))`
when Hash
Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
else
value
end
end

Converting snake case to normal sentence in Ruby

humanize is your thing:

[4] pry(main)> "hello_world".humanize
"Hello world"

Problems with CamelCase and underscore Rails 3

You need to follow the Rails convention and use down-cased names when you refer to models when defining relations:

 TipoDocumento < ActiveRecord::Base
has_many :dependencias
has_many :tipo_requisitos, through: :dependencias
...
end

TipoRequisito < ActiveRecord::Base
has_many :dependencias
has_many :tipo_documentos, :through => :dependencias
...
end

Dependencia < ActiveRecord::Base
belongs_to :tipo_documento
belongs_to :tipo_requisito
...
end

you need to lower-case it, like this:

  x = TipoDocumento.find(1)
x.tipo_requisitos

Please also check: http://guides.rubyonrails.org/association_basics.html



Related Topics



Leave a reply



Submit