views:

48

answers:

2

Hi,

I want to create a rails' model like FileURL without having to name the file file_u_r_l.rb. How does one go about doing something like this? I understand that you can set table name explicitly but how does one override the default filename to classname mapping?

Thanks Prateek

+1  A: 

You don't need to do anything. Rails' .underscore method translates 'FileURL' to 'file_url', and will look for a file with the same base name in the load path.

If ActiveRecord does not guess the correct table name, use set_table_name:

class FileURL < ActiveRecord::Base
  set_table_name "file_urls"
  ..
end
Steven Soroka
That works for setting table name but I am not sure if that will work for filename -> classname. I am trying to create a class called IMAPMbox but unless I name the file i_m_a_p_mbox.rb, rails throws a "Expected blah to define blah" exception.
prateekdayal
That's a result of lazy loading. You could always just explicitly require the file first so the constant is already defined. Seems odd that it would expect i_m_a_p_mbox.rb, I've never seen it do that before.
Steven Soroka
I think you are right. There was something funny going on between factory_girl, rails and rspec. I have it all sorted out with a sane filename like imap_mbox.rb but something like Factory.define :i_m_a_p_mbox !!
prateekdayal
+2  A: 

Ask yourself whether this is really a requirement in the first place. In general, you're best off going with the Ruby (and Rails) conventions rather than fighting them. Why not just name the model class FileUrl?

Mirko Froehlich