I don't found anything on this issue.
I create migration like this :
rails generate migration MaBase
And I fill the *_ma_base.rb like that
class MaBase < ActiveRecord::Migration
def change
create_table :connexion
add_column :connexion, :ip, :string
add_column :connexion, :user_agent, :string
add_column :connexion, :nb, :integrer
add_column :connexion, :date, :datetime
create_table :recherche
add_column :recherche, :ip, :string
add_column :recherche, :recherche, :string
add_column :recherche, :date, :datetime
create_table :membre
add_column :membre, :ip, :string
end
end
And I get the error.
After reading some subject here, and i create create a file in model ma_base.rb with this kind of code :
class MaBase < ActiveRecord::Base
end
But the error is stil here
Thanks for support
Change your class name in migration file from MaBase to MaBases. Activerecord maps plural of model names in db files. Try this:
class MaBases < ActiveRecord::Migration
Related
I've read a few guides on adding an attribute to a Model in rails, but none seem to specify the Model you're affecting in the Migration.
I'd like to add an image_url property to our coffees model, but the migration examples I've seen don't specify a model. What would I need to do to get this working correctly?
Well the migration api is pretty clear:
add_column :table, :column_name, :column_type
Example:
add_column :coffees, :image_url, :string
you create a new migration file to add image_url in Coffee models retaled table.
if you write
rails g migration AddImageUrlToCoffees image_url:string
then a migration file will be generated like
class AddImageUrlToCoffees < ActiveRecord::Migration
def change
add_column :coffees, :image_url, :string
end
end
or
class AddImageUrlToCoffees < ActiveRecord::Migration
def up
add_column :coffees, :image_url, :string
end
def down
remove_column :coffees, :image_url, :string
end
end
when you run rake db:migrate then it will add one more column image_url in coffees table and it can be access from model Coffee.
Run this command in the console
rails generate migration AddImageUrlToCoffees image_url:string
it will generate for you with
class AddImageUrlToCoffees < ActiveRecord::Migration
def change
add_column :coffees, :image_url, :string
end
end
I recently added new columns to my database (sqlite) for Media and it shows that the columns are inserted, but the new columns will not update on Medium.new
My original database:
class CreateMedia < ActiveRecord::Migration
def change
create_table :media do |t|
t.string :name
t.string :location
t.timestamps
end
end
end
These columns update on Media.new
class AddMetaToMedia < ActiveRecord::Migration
def change
add_column :media, :ext, :string
add_column :media, :l_mod, :string
add_column :media, :d_create, :string
end
end
and I am calling
Medium.new(name: f, location: str, ext: ex)
ext will not update to ex = File.extname(f), which I know has a value through print statements/console. Am I calling Medium.new wrong? Why is it updating name and location but not the new columns?
edit: Here is my model, I've tried with and without attr_accessible/attr_accesor
class Medium < ActiveRecord::Base
attr_accessor :ext, :d_create, :l_mod
end
Mostly for those who find this later...may also need to whitelist new params in interested controllers
attr_accessor :ext, :d_create, :l_mod remove this line from your model and try again.
Now you have these attributes in DB so Rails will do this job automatically
I want to add a new column for a bunch of different items on my site.
class AddCreatedatToStreamItems < ActiveRecord::Migration
def change
add_column :stream_artworks, :createdat, :date
add_column :stream_experiments, :createdat, :date
add_column :stream_photographies, :createdat, :date
add_column :stream_webs, :createdat, :date
add_column :stream_socials, :createdat, :date
end
end
Any chance the filename of the migration is "123123_add_dates.rb" instead of "123123_add_createdat_to_stream_items.rb" (where '123123' is a string of digits, not necessarily '123123')?
The classname of the migration and the filename need to match in the CamelCase => camel_case way.
Can anyone show me how to edit the following migration to change :phone integer to string?
class CreateContactInfos < ActiveRecord::Migration
def change
create_table :contact_infos do |t|
t.integer :phone
t.string :facebook
t.references :user
t.timestamps
end
add_index :contact_infos, :user_id
end
end
Thanks in advance!
I guess you already migrated the one you're showing, so create another in which you'd put:
change_column :contact_infos, :phone, :string
I have added some more explanation to this.We need to generate a new migration
rails g migration change_phone_to_be_string_in_contact_infos
If we open up the migration we should see something like this
class ChangePhoneToBeStringInContactInfos < ActiveRecord::Migration[5.0]
def change
end
end
What we call this migration will have no impact on what we need to do next, but future we and other developers will thank us for naming our migration appropriately.
As you can see the change method is sitting empty. We need to manually add some code here.
class ChangePhoneToBeStringInContactInfos < ActiveRecord::Migration[5.0]
def change
change_column :customers, :phone, :string
end
end
After Saving this file just do rake db:migrate we can see changes we want.
For a reversible migration, use:
def up
change_column :contact_infos, :phone, :string
end
def down
change_column :contact_infos, :phone, :integer, using: "phone::integer"
end
Convert column type string into integer in rails migration :
def change
change_column :contact_infos, :phone, :integer, using: 'phone::integer'
end
Convert column type integer into string in rails migration:
def change
change_column :contact_infos, :phone, :string, using: 'phone::string'
end
Im trying to set up my app to work with authlogic.. the thing is, other than the fields authlogic is supposed to use I want to keep in my database other attributes
Like Name, Last Name, PIN, etc... is there a way to do this?
You say "keep" - do you mean you have an existing database of users and you want to keep this info as you migrate to AuthLogic, or do you mean you just want to store this additional info?
Either way is possible but I'm going to assume you mean that you just want to store additional information - all you have to do is script/generate migration AddFieldsToUser then edit the migration:
class AddFieldsToUser < ActiveRecord::Migration
def self.up
add_column :users, :name, :string
add_column :users, :last_name, :string
add_column :users, :pin, :integer
end
def self.down
remove_column :users, :name
remove_column :users, :last_name
remove_column :users, :pin
end
end
Then run rake db:migrate