Ruby on Rails: how to transfer changes made on models?

In a Rails application, how can I transfer the changes I make to models? For example, I know that if I create a model with the command "rails g model Person name: string", a migration will be created. However, if after this step I go to the created Man model and add a new attribute, will this new attribute be automatically added to the transfer for subsequent resistance in the database? Or am I looking at it the wrong way and the attribute must be added to the hyphen and then added to the model?

Hi

+8
ruby ruby-on-rails ruby-on-rails-3 rails-migrations rails-models
source share
3 answers

You cannot “add” a model attribute; you do this by creating a migration file and launching it. Rails determines which model-based attributes are based on columns in the database. However, you need to add a row to the model for the attribute whitelist if you want to update it with a mass assignment. This is why you often see this line in activerecord models:

attr_accessible :name 

But it is optional and not necessary to add an attribute.

To add a new attribute to your model, first create a migration with:

 rails g migration AddAddressToPerson address:string 

This will create a migration file in the db / migration / directory. (The forms "AddXXXToYYY" and "RemoveXXXFromYYY" are understood as rails to indicate "add (or remove) a new column to model XXX", see the Documentation). In this case, I added an attribute called address , which is a string, but you can change it the way you want.

Then, to actually upgrade the database, you need to migrate using rake :

 rake db:migrate 

Finally, if you want to allow bulk assignment to this attribute, add this attribute to the attr_accessible argument attr_accessible :

 attr_accessible :name, :address 

That should do it.

+9
source share

If you add the new attr_accessor attribute, you do not need to do anything with the migrations, but your changes will not be stored in the database.

If you want to save your changes, you need to add an attribute to your model using migration . You can simply create a text file with the appropriate structure, migrations do not represent anything, but it is much easier to generate, for example, it rails generate migration AddLastNameFieldToUsers . The contents of such a file can be configured as follows:

 class AddLastNameFieldToUsers< ActiveRecord::Migration def change add_column :users, :last_name, :string end end 
+2
source share

You do not need to add attributes directly to the model. Rails (actually ActiveRecord) automatically passes it. For a list of attributes for a model class, AR looks for a table with the plural form of the model name (if the model is Order, it will look for attributes in the order table). This is part of the CoC design function - Convention over Configuration.

So, if you need to add an attribute, you need to create a migration to add this field to the column, as indicated in other answers.

0
source share

All Articles