Forum: Ruby on Rails Overriding Model Name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Doug J. (Guest)
on 2009-04-25 02:38
(Received via mailing list)
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
Philip H. (Guest)
on 2009-04-25 02:50
(Received via mailing list)
> 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
Doug J. (Guest)
on 2009-04-25 06:17
(Received via mailing list)
> 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
Philip H. (Guest)
on 2009-04-25 07:44
(Received via mailing list)
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.
Doug J. (Guest)
on 2009-04-26 00:39
(Received via mailing list)
> 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
This topic is locked and can not be replied to.