Overriding Model Name


#1

If I understand things correctly, by default, Rails expects both the
ActiveRecord class name and the model name (i.e., the name of the file
that contains the definition of the ActiveRecord class) to be the
singluar of the table name. How can I override this default behavior
with respect to the model name? That is, I would like to have the
file name of the file that contains the definition of the ActiveRecord
class to be something totally different from the table name. Thanks
for any input.

        ... doug

#2

If I understand things correctly, by default, Rails expects both the
ActiveRecord class name and the model name (i.e., the name of the file
that contains the definition of the ActiveRecord class) to be the
singluar of the table name. How can I override this default behavior
with respect to the model name? That is, I would like to have the
file name of the file that contains the definition of the ActiveRecord
class to be something totally different from the table name. Thanks
for any input.

app/models/apple.rb:

class Apple < ActiveRecord::Base
set_table_name ‘oranges’
end


#3

app/models/apple.rb:

class Apple < ActiveRecord::Base
set_table_name ‘oranges’
end

I understand that I can set the table name from within the class. It
just seemed to me that, in addition to that, I would have to somehow
alert Rails to the fact that the model for the ‘oranges’ table was
contained in a file named ‘apple.rb’. Otherwise, how would Rails know
to look for the Apple model in a file named apple.rb when the name of
the table is oranges? Put differently, I thought that by default,
Rails looked for the model in a file with a name which was the
singular of the table name. I don’t see how I can tell it from within
a different file to look for that different file as it would have to
have found the different file to know that in the first place. Am I
making any sense? Do you see why I am confused?

Thanks for the input.

     ... doug

#4

You’re looking at it backwards…

You’re thinking this: database table -> rails -> model

The reality is: rails -> model -> database table

Rails will load up all of the files in app/models and assumes by
default that there is a pluralized database table to go with each
model. set_table_name changes that assumption.

Wow! What a revelation! Thanks a batch.

      ... doug

#5

On Apr 24, 2009, at 7:16 PM, djolley wrote:

contained in a file named ‘apple.rb’. Otherwise, how would Rails know
to look for the Apple model in a file named apple.rb when the name of
the table is oranges? Put differently, I thought that by default,
Rails looked for the model in a file with a name which was the
singular of the table name. I don’t see how I can tell it from within
a different file to look for that different file as it would have to
have found the different file to know that in the first place. Am I
making any sense? Do you see why I am confused?

You’re looking at it backwards…

You’re thinking this: database table -> rails -> model

The reality is: rails -> model -> database table

Rails will load up all of the files in app/models and assumes by
default that there is a pluralized database table to go with each
model. set_table_name changes that assumption.