Migration to add enum column in rails3 and enumerated_attribute - ruby-on-rails-3

I need a migration to add column of type enum in rails 3. I will be using enumerated_attribute gem.
I generated a migration to add the column:
rails generate migration addUsage_reports_accessToClientParam usage_reports_access:enum
Now I need to set up the values for the enum and set the default value. Here is the generated migration:
class AddUsageReportsAccessToClientParam < ActiveRecord::Migration
def self.up
add_column :client_params, :usage_reports_access, :enum
end
def self.down
remove_column :client_params, :usage_reports_access
end
end
Thanks

I found a solution. This works:
add_column :client_params, :usage_reports_access, "ENUM('value1','value2', 'value3') DEFAULT 'value1'"

Related

RoR: how can I add columns to my database on my live heroku app?

I am trying to add :price, :location, and :product to the columns for my microposts table. I have already done a bunch of other migrations and I have heard that rolling back all of migrations and redoing them is error prone. So I guess the other option is the schema file? I have heard that the schema file is just to be read and not edited. I have been looking at http://guides.rubyonrails.org/migrations.html but can't find the right info. They briefly talk about change_table which I think could be useful but it doesn't go into depth. Is this what I am looking for?
Just create a new standalone migration:
rails g migration add_price_location_and_product_to_microposts
It will create a file in the db/migrate folder, edit it:
def change
add_column :microposts, :price, :float # dont forget to change the type to the columns
add_column :microposts, :location, :string
add_column :microposts, :product, :integer
end
(You can define the change method, instead of up and down because add_column is a reversible command.)
And then, run rake db:migrate

How to use hstore on Heroku

As per https://postgres.heroku.com/blog/past/2012/4/26/heroku_postgres_development_plan/ I did "heroku addons:add heroku-postgresql:dev". But when I do
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
execute "CREATE EXTENSION hstore"
t.hstore :access
end
end
def down
drop_table :users
execute "DROP EXTENSION hstore"
end
end
end
and then "heroku run rake db:migrate" I get this error:
PG::Error: ERROR: syntax error at or near "EXTENSION"
LINE 1: CREATE EXTENSION hstore ^
: CREATE EXTENSION hstore
finally got it to work. turns out i needed to "promote" the database using heroku pg:promote as per https://devcenter.heroku.com/articles/heroku-postgres-dev-plan
I think you want to split out your migrations, one to add hstore, the other to use it;
class SetupHStore < ActiveRecord::Migration
def self.up
execute "CREATE EXTENSION hstore"
end
def self.down
execute "DROP EXTENSION hstore"
end
end
to enable the extension, and the your Users migration will just add any fields and then use hstore on which ever column you want.

How to add a Hash object to an ActiveRecord class? Tried but migration fails

I want my ActiveRecord class User to contain options (a bunch of string key-values), so I wrote:
rails generate migration AddOptionsToUser options:Hash
It generated:
class AddOptionsToUser < ActiveRecord::Migration
def self.up
add_column :users, :options, :Hash
end
def self.down
remove_column :users, :options
end
end
I also added this line to my class User:
serialize :options, Hash
But the migration fails:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Hash' at line 1: ALTER TABLE `users` ADD `options` Hash
I am new to Rails, what is the usual way to store a bunch of string key-values in an ActiveRecord class?
Rails serializes things in to a (YAML) string. So in your database, the type should be string (or text).
class AddOptionsToUser < ActiveRecord::Migration
def self.up
add_column :assessments, :options, :string
end
def self.down
remove_column :assessments, :options
end
end
To have ruby object as an attribute of the ActiveRecord model you should use serialize method inside your class for that attribute link

rails3 migration to add new column does not save values to table

So, I created the following migration:
class AddAclToUsers < ActiveRecord::Migration
def self.up
add_column :users, :acl, :integer
end
def self.down
remove_column :users, :acl
end
end
however, after modifying the various erb files in the view, values entered in edit.html.erb are not saved to the database.
I can manually start SQlite3 and select * the table and see that the column was created but no values are entered. I can also manually UPDATE or INSERT numbers into the new column which the controller queries and displays correctly.
Any suggestions on what may be wrong with the frameworks udate/save??
-daniel
Please check if there is a list of attr_accessible in your User model. If you are using Devise like gem/plugin for authentication you would have a list of attr_accessible in the model.
Add new attribute (acl in your case) to the attr_accessible list.
If this is not the case, I would like you to paste your view, controller and model code

Default value not populating in migration with Rails and Postgresql

I am currently trying to run this migration:
class AddDroppedProjectsCountToUser < ActiveRecord::Migration
def self.up
add_column :users, :dropped_projects, :integer, {:default=>0, :required=>true}
end
def self.down
remove_column :users, :dropped_projects
end
end
The column is added correctly, but none of the old records are populated with 0. They are nil. I have tried using default=>'0' as well, to no avail. Any idea why this might be happening? (Rails 3.0.3)
Edited to add: When I create a new user it works fine, and everything looks correct. It's just the old users that still have nil for that value in the table.
What happens if you say:
def self.up
add_column :users, :dropped_projects, :integer, :null => false, :default => 0
end
instead? Without the :null=>false you're still allowing NULL in dropped_projects so there's no reason for PostgreSQL to make them 0. Also, I don't think :required is a valid option for add_column; since the options are just a simple Hash and add_column only looks for options it knows about, your stray :required option is silently ignored.
you could do this:
(taken from http://apidock.com/rails/ActiveRecord/Migration)
Using a model after changing its table
Sometimes you’ll want to add a column in a migration and populate it
immediately after. In that case, you’ll need to make a call to
Base#reset_column_information in order to ensure that the model has
the latest column data from after the new column was added. Example:
class AddPeopleSalary < ActiveRecord::Migration
def up
add_column :people, :salary, :integer
Person.reset_column_information
Person.all.each do |p|
p.update_column :salary, SalaryCalculator.compute(p)
end
end
end
I believe this is due to the fact that you are changing the old migration, instead of creating a new.
In this case, the solution is to check the schema file (schema.rb). It does not change automatically, and add
t.integer "dropped_projects", default: 0, null: false