model and table relationship

82 views
Skip to first unread message

fredrated

unread,
May 18, 2011, 10:37:34 AM5/18/11
to Ruby on Rails: Talk
According to 'Agile web development with rails' "... a model is
automatically mapped to a database table whose name is the plural form
of the model's class". Sure enough, when I created migrations for
'Tc_project', 'Tc_employee' and 'Tc_period' the migration files issue
a 'create table' with the table names pluralized. However, when I
issue "Rails generate scaffold Tc_data..." the create table statement
in the migration DID NOT pluralize 'tc_data' to 'tc_datas'!

Does anyone know if this will create a 'convention' problem between
model and table references for the table 'Tc_data'?
Unless I hear otherwise I will assume Rails knows what it is doing and
run the migration as-is and proceed as if nothing is out of the
ordinary. If Rails chokes on this I will post a reply to my own
question.

Thanks in advance for any help. Fred

Tim Shaffer

unread,
May 18, 2011, 10:49:41 AM5/18/11
to rubyonra...@googlegroups.com
Rails is smart enough to know that the plural of some words are irregular (examples: category, person) and that some words are "uncountable" (examples: money, information).

Since "data" is already the plural form of "datum" I think Rails knows not to make it "datas".

You should be good to go.

Colin Law

unread,
May 18, 2011, 11:32:43 AM5/18/11
to rubyonra...@googlegroups.com

As Tim has pointed out, this should be datum for the singular and data
for the plural. I would also point out that you will make life easier
for yourself if you stick to the Rails conventions for capitalisation
and underscores. In this case the class would be something like
TcDatum and the table tc_data (or something completely different if
you do not like datum). Similarly I would change the names of the
other classes you mention.

Colin
Colin

fredrated

unread,
May 18, 2011, 12:56:48 PM5/18/11
to Ruby on Rails: Talk
"you will make life easier for yourself if you stick to the Rails
conventions for capitalisation and underscores."

Actually I was trying to do that. My boss told me (he is also a
newbie) that table names start with a capital letter, though I cannot
find documentation to that effect.
If you can point me to any references to naming conventions I would
appreciate it, thanks.

I start each table with "tc_" because I am coming from Drupal where
all tables of all modules are in one database and this is necessary to
prevent name conflicts.

All help is greatly appreciated. -Fred

loganathan sellappa

unread,
May 18, 2011, 1:07:22 PM5/18/11
to rubyonra...@googlegroups.com
Hi All,
 I want to create an application in order to convert web page in to pdf.
like the below
I/p => http://www.test.com
o/p => pdf file has the content of http://www.test.com page.
Is their any plugins available for this option.

Thanks in avance


Thanks & Regards,
Loganathan

Kendall Gifford

unread,
May 18, 2011, 4:14:35 PM5/18/11
to rubyonra...@googlegroups.com
On Wednesday, May 18, 2011 10:56:48 AM UTC-6, fredrated wrote:
"you will make life easier for yourself if you stick to the Rails
conventions for capitalisation and underscores."

Actually I was trying to do that.  My boss told me (he is also a
newbie) that table names start with a capital letter, though I cannot
find documentation to that effect.
If you can point me to any references to naming conventions I would
appreciate it, thanks.

Table names are all lowercase by convention (and plural). Model names are singular, capitalized CamelCase. In fact, it is a ruby-wide convention that class names are capitalized CamelCase (not just rails).
 

I start each table with "tc_" because I am coming from Drupal where
all tables of all modules are in one database and this is necessary to
prevent name conflicts.


As for having a prefix, you might be interested in the #table_name_prefix property:

http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-table_name_prefix

This would allow you to have tables named:

tc_projects
tc_employees
tc_periods
tc_datum

And model classes respectively:

class Project < ActiveRecord::Base
  table_name_prefix = "tc_"
end
...
class Datum < ActiveRecord::Base
  table_name_prefix = "tc_"
end

In order to then DRY up your code a bit, you *might* be able to use an abstract base class:

class TcPrefix < ActiveRecord::Base
  table_name_prefix = "tc_"
  abstract_class = true
end

class Project < TcPrefix
end
...
class Datum < TcPrefix
end

I haven't tested this to see if this variation works correctly. I'd give a try though.

Kendall Gifford

unread,
May 18, 2011, 4:22:56 PM5/18/11
to rubyonra...@googlegroups.com

tc_datum


Woops, table name would be tc_data while the class would be Data (or TcData if you don't use table_name_prefix). My bad.

Kendall Gifford

unread,
May 18, 2011, 4:29:37 PM5/18/11
to rubyonra...@googlegroups.com
On Wednesday, May 18, 2011 2:22:56 PM UTC-6, Kendall Gifford wrote:

tc_datum


Woops, table name would be tc_data while the class would be Data (or TcData if you don't use table_name_prefix). My bad.

Oh my gosh. I need some sleep and to actually proof-read what I type before I click "Send".

Table name: tc_data
Model name: Datum or TcDatum

(crawling back into my hole...)
Reply all
Reply to author
Forward
0 new messages