Rails Generate Model Primary Key
Join GitHub today
Rails will generate a name for every foreign key starting with fkrails followed by 10 characters which are deterministically generated from the fromtable and column. There is a:name option to specify a different name if needed. Generate a pair of ssh keys. Active Record only supports single column foreign keys. Jan 31, 2018 Creating a Model The most convenient way to create a model in Rails is to use a generator. We do this by going to the command line inside our application directory and running a Rails generate statement where we name the model we want to create along with the column names and the data type that will reside in each of those columns. The key references the id column of the authors table. If the column names can not be derived from the table names, you can use the:column and:primarykey options. Rails will generate a name for every foreign key starting with fkrails followed by 10 characters which are deterministically generated from the fromtable and column.
Winzip 21.5 key generator. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upBranch:master
# frozen_string_literal: true |
require'rails/generators/active_record' |
moduleActiveRecord |
moduleGenerators# :nodoc: |
classModelGenerator < Base# :nodoc: |
argument:attributes,type: :array,default: [],banner: 'field[:type][:index] field[:type][:index]' |
check_class_collision |
class_option:migration,type: :boolean |
class_option:timestamps,type: :boolean |
class_option:parent,type: :string,desc: 'The parent class for the generated model' |
class_option:indexes,type: :boolean,default: true,desc: 'Add indexes for references and belongs_to columns' |
class_option:primary_key_type,type: :string,desc: 'The type for primary key' |
class_option:database,type: :string,aliases: %i(--db),desc: 'The database for your model's migration. By default, the current environment's primary database is used.' |
# creates the migration file for the model. |
defcreate_migration_file |
returnunlessoptions[:migration] && options[:parent].nil? |
attributes.each{aa.attr_options.delete(:index)ifa.reference? && !a.has_index?}ifoptions[:indexes]false |
migration_template'././migration/templates/create_table_migration.rb',File.join(db_migrate_path,'create_#{table_name}.rb') |
end |
defcreate_model_file |
template'model.rb',File.join('app/models',class_path,'#{file_name}.rb') |
end |
defcreate_module_file |
returnifregular_class_path.empty? |
template'module.rb',File.join('app/models','#{class_path.join('/')}.rb')ifbehavior:invoke |
end |
hook_for:test_framework |
private |
defattributes_with_index |
attributes.select{a !a.reference? && a.has_index?} |
end |
# Used by the migration template to determine the parent name of the model |
defparent_class_name |
options[:parent]'ApplicationRecord' |
end |
end |
end |
end |
Rails Generate Model Primary Key Definition
Copy lines Copy permalink