module ActiveSupport::Inflector
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
foreign_key('Message', false) # => "messageid"
foreign_key('Message') # => "message_id"
the method should put '_' between the name and 'id'.
+separate_class_name_and_id_with_underscore+ sets whether
Creates a foreign key name from a class name.
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") end