rails attribute names camelcase issue

帅比萌擦擦* 提交于 2019-12-06 00:39:53

Maybe set up aliases for all defined column names?

column_names.each do |column_name|
  alias_attribute column_name.underscore, column_name
end

You could add that to a concern and include that concern in all relevant models.

I don't think there is a single option that would switch on such behavior. Rails, after all, obeys the convention over configuration principle but not all internal conventions are configurable.

On the other hand you should be able to define column aliases for all of your columns, e.g:

class Customer < ActiveRecord::Base
   alias_attribute :first_name, :FirstName
   # etc...
end

This way Rails should transparently "translate" the underscore attributes to CamelCase ones:

Customer.where(first_name: "John")
# => SELECT `customers`.* FROM `customers` WHERE `customers`.`FirstName` = 'John'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!