instance method
foreign_key
Ruby on Rails 8.1.2
Since v2.2.3Signature
foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
Creates a foreign key name from a class name. separate_class_name_and_id_with_underscore sets whether the method should put ‘_’ between the name and ‘id’.
foreign_key('Message') # => "message_id" foreign_key('Message', false) # => "messageid" foreign_key('Admin::Post') # => "post_id"
Parameters
-
class_namereq -
separate_class_name_and_id_with_underscoreopt = true
Source
# File activesupport/lib/active_support/inflector/methods.rb, line 267
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
Defined in activesupport/lib/active_support/inflector/methods.rb line 267
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::Inflector