rake db:migrate issue postgresql - ruby-on-rails

I'm complete noob at PostgreSQL, Ruby on Rails..
I'm trying to follow this tutorial (WITHOUT rubymine) http://www.codeproject.com/Articles/575551/User-Authentication-in-Ruby-on-Rails#InstallingRubyMine4
I have as such this to migrate (001_create_user_model.rb):
class CreateUserModel < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :username, :string
t.column :email, :string
t.column :password_hash, :string
t.column :password_salt, :string
end
end
def self.down
drop_table :users
end
end
The error I'm getting goes like this:
syntax error, unexpected ':', expecting ';' or '\n'
t.column...sers do |t|
...
C:131071:in 'disable_dll_transaction'
Task:TOP => db:migrate

What about this:
class CreateUserModel < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :username, :null => false
t.string :email,:null => false
t.string :password_hash, :null => false
t.string :password_salt, :null => false
t.timestamps
end
end
def self.down
drop_table :users
end
end

Related

Sort columns active scaffold

I am trying to list/order my columns in a custom order with active scaffold, Rails 5.1.2 and Ruby 2.3.3p222.
Migration as follows:
class CreateResources < ActiveRecord::Migration[5.1]
def change
create_table :resources do |t|
t.string :firstname
t.string :surname
t.date :date_of_birth
t.string :identity_number
t.string :nationality
t.string :state_province
t.date :interview_date
t.string :available
t.string :home_phone
t.string :mobile_phone
t.timestamps
end
end
end
And I have added columns, the migration is as follows:
class AddColumnsToResources < ActiveRecord::Migration[5.1]
def change
add_column :resources, :expected_salary, :string
add_column :resources, :current_ctc, :string
add_column :resources, :years_of_experience, :string
add_column :resources, :notice_period, :string
end
end
I then used paperclip to add an attachment to the table:
class AddAttachmentAttachmentToResources < ActiveRecord::Migration[5.1]
def self.up
change_table :resources do |t|
t.attachment :attachment
end
end
def self.down
remove_attachment :resources, :attachment
end
end
I need to sort my columns so they appear in the following order:
firstame
surname
identity number
...

Getting build task error while running rake db:migrate in rails 3

I am getting the following error while running command
rake db:migrate VERSION=201504******.
Error:
rake aborted!
Don't know how to build task '20150419131135'
Actually i have a migration file already.Now i am connecting to a new database .Here i want to create table in that DB.Please check my below file.
20150419131135_create_users.rb:
class CreateUsers < ActiveRecord::Migration
def self.up
if !table_exists? :users
create_table :users do |t|
t.string :contact_name
t.string :login_id
t.string :password_hash
t.string :password_salt
t.string :phone
t.string :address
t.timestamps
end
end
end
end
class CreateUsers < ActiveRecord::Migration
def self.down
drop_table :users if !table_exists?(:users)
end
end
After migrate this I got the above error.Please help me to resolve this error.
self.down and self.up methods should be within one CreateUsers class:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :contact_name
t.string :login_id
t.string :password_hash
t.string :password_salt
t.string :phone
t.string :address
t.timestamps
end unless table_exists? :users
end
end
def self.down
drop_table :users if table_exists? :users
end
end

How to inherit from another Rails migration?

I have a Rails migration for a simple User model:
class Users < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name, :default => :null
t.float :weight
t.datetime :recorded_at
t.timestamps
end
end
end
I would like to have a second table for the history of the user. It should have the same columns but another name, obviously. Also it should reference the user table.
require_relative '20130718143019_create_history.rb'
class History < Users
def change
create_table :history do |t|
t.references :user
# ...?
end
end
end
How can use inheritence to avoid copying all the migration configuration?
After leaving the keyboard tomatoes fell off my eyes and it was clear how I can set this up:
class Users < ActiveRecord::Migration
def change
create_table :users do |t|
prepare_columns(t)
end
end
protected
def prepare_columns(t)
t.string :name, :default => :null
t.float :weight
t.datetime :recorded_at
t.timestamps
end
end
...
require_relative '20130718143019_create_history.rb'
class History < Users
def change
create_table :history do |t|
t.references :user
prepare_columns(t)
end
end
end

Ruby on Rails + devise: How can i create customize users table with devis rake db:migrate?

rails generate devise User I got this=>
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
# t.encryptable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
drop_table :users
end
end
But i want to create a users table with username, email, password, role, group, mark, created_at, modified_at columns.
How can i do this ?
Is this structure correct to have username, password, email, group, role, mark ?
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
# t.encryptable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.string :username
t.string :password
t.string :email
t.string :group
t.string :role
t.integer :mark
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
drop_table :users
end
end
What are these?
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
You can perform a migration to add some fields to the user table.
For example:
rails g add_fields_to_users username:string # as well as other fields you need
Then, in order to add columns to your table run:
rake db:migrate
Devise has already generated some columns you need like: email, password, created_at, updated_at...
For adding roles to your user model you should watch the cancan screencast: railscasts and also read the doc in order to see some updates.
EDIT:
If you want to add fields manually you could add them in your self.up method before running your migration:
def self.up
create_table(:users) do |t|
#...
t.rememberable
t.trackable
t.string :username
#... your other attributes here
end

Why migration doesn't work

I have a migration AddAuthenticableToUser. (rake db:migrate:up VERSION=..) works fine, but when I'm trying to rollback a migration (rake db:migrate:down VERSION=..) it doesn't works. Any errors or warnings. Could you help me with this ?
def self.up
change_table :users do |t|
t.token_authenticatable
end
add_index :users, :authentication_token, :unique => true
end
def self.down
remove_index :users, :authentication_token
remove_column :users, :authentication_token
end
This should be the trick. I think you named your table token_authenticatable and then tried to remove authentication_token.
def self.up
create_table :reviews do |t|
t.column :authentication_token
end
add_index :users, :authentication_token, :unique => true
end
def self.down
remove_index :users, :authentication_token
remove_column :users, :authentication_token
end

Resources