0

At my company, we're about to start out first Ruby On Rails application and it will have a new database. All of our current databases conform strictly to the Upper Camel Case naming convention for tables and columns (i.e MyNiceTable). Discussions I've had with Ruby On Rails programmers have concluded that for this new application we should switch to Snake Case for our database (i.e. my_nice_table). I never really got an answer as to why though, other than that's how it's done.

I'm uncomfortable with this for a few reasons. First, it won't conform to our existing conventions. Second, it means we're doing database design based on the application design/programming language choice, which we strongly try to avoid. And finally third, other applications written in other languages will most likely be using this database in the future as well, so why would we change our convention just for this one application? Shouldn't be necessary to me. I mean, what if we were working with an existing database that couldn't be changed?

But I've been told that due to how Active Record works, it will be much easier if we use snake case. We're new to ROR over here, so we also don't want to make things extra complicated for ourselves for no reason.

So, what's the truth? Is there any reason, other than "this is how it's typically done", that we need our database to use snake case instead of upper camel case? If there are reasons, are they easily overcome through other means?

Thanks

Nullqwerty
  • 1,140
  • 1
  • 20
  • 37
  • Naming conventions are nothing more than choosing some logic for default values. You can ignore it at your needs, as @KiChjang pointed out. – mdesantis Feb 21 '14 at 15:32

2 Answers2

2

You're right when you said Snake Case is the convention. However, at the same page of the reference that I've provided, it also tells you how to override the naming conventions. Here's a code example, directly from the website:

class Product < ActiveRecord::Base
  self.table_name = "PRODUCT"
end
Ki Chjang
  • 311
  • 3
  • 9
  • Very cool, thanks. Great links too. So, quick question on it, if you know the answer. Lets say I have a database table called Products with a primary key column called ProductID, and another column called ProductName. I now know that I can use self.table_name = "Product" and self.primary_key = "ProductID". But, will ROR, understand my ProductName column? – Nullqwerty Feb 21 '14 at 15:32
  • I believe you are trying to ask whether a Rails model would recognize your database columns? I'm not quite sure of the answer to that, but [this here](http://guides.rubyonrails.org/migrations.html#running-migrations) explains that you will need to use ``rake db:migrate:dump`` to get Rails to read your existing database schema. After that, try the suggestion posted [here](http://stackoverflow.com/questions/4119659/rails-3how-to-generate-models-for-existing-database-tables/4120506#4120506). – Ki Chjang Feb 21 '14 at 15:52
  • Rails doesn't have problems about it, but you can't use model getters/setters `record.ColumnName / record.ColumnName=(...)`, since Ruby mistakes them for constants. Anyway you can use `send(:ColumnName)/=(...)`, `read/write_attribute(:ColumnName)`, `attribute[:ColumnName]/=()`, and create aliases at your will, f.e. `def column_name; send(:ColumnName); end` and `def column_name=(value); send(:ColumnName=, value); end` – mdesantis Feb 21 '14 at 16:01
  • I'm sorry, you *can* use them as getters/setters: `Person.first.FirstName = "Francesco"; Person.first.FirstName #=> "Francesco"` – mdesantis Feb 21 '14 at 16:17
1

I really believe that a Framework is just a tool that you use, nothing else. Rails is well known because its "opinionated" way of doing stuff, but I would advise your company to not twist business or design decisions because of a tool.

If you have conventions in your company or design decisions that your tech team made, you shouldn't let a framework mess with them and, if does, it is not the right tool for you to use. Rails can be customized in terms of naming conventions without much hassle, so for your case specifically, this is not an issue.

I always try to remind specially Ruby developers (there are many out there who learned to use Rails before learning how to write code in Ruby) that we are primarily Software Developers (or Software Engineers, whatever) and not Rails Developers, or Sinatra Developers or [put the framework name here] Developers.

It is good to use a Framework with conventions, easy to use, extend and well maintained, like Rails. It is not good though, to surrender yourself to one way to think about overall web application design.

Anyway, I found an answer that points specifically to change the "snake_case" database conventions in Rails. I think it could be helpful:

Ruby on Rails: Is it possible to use Camel-cased database field and table names?

Community
  • 1
  • 1
Guilherme Franco
  • 1,465
  • 12
  • 19
  • Thanks ! As a matter of fact, it is nice to see questions like the one you wrote. It shows there are still developers who think about a framework as a way to get things done, not an "answer for all questions" thing. – Guilherme Franco Feb 21 '14 at 15:34
  • Well, not really, the place for "conceptual questions" about programming is [Programmers Stack Exchange](http://programmers.stackexchange.com/), StackOverflow is for practical questions – mdesantis Feb 21 '14 at 15:41
  • Yes I do :) it doesn't involve code, so it is conceptual to me – mdesantis Feb 21 '14 at 15:44
  • We agree to disagree on that one ! ;) – Guilherme Franco Feb 21 '14 at 15:49