开发者

How to name a model something like FileURL

开发者 https://www.devze.com 2023-01-19 02:52 出处:网络
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 ho

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?

开发者_C百科

Thanks Prateek


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号