Rails 4 Migration - ruby-on-rails

I had a table posts with column content of string type(255), when i migrate changes,to change type of string to text,it really change that type,but i get text(255) what does nothing.What im must to do to get such result :
TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)2
p.s. on my localmachine i can create posts of any long and type of string,but on heroku i get
PG::Error: ERROR: value is too long for type character variyng(255)
my _change_datatypes_on_posts_from_string_to_text.rb
class ChangeDatatypeOnPostsFromStringToText < ActiveRecord::Migration
def change
change_column :posts, :content, :text
change_column :posts, :title, :text
change_column :users, :name, :text
end
end

Try using the limit attribute...
eg : "change_column :posts, :content, :text, limit: nil".
On Postgres, a :string, limit:nil is effectively synonymous with :text.

Related

Remove boolean and add list in ruby on rails

I would like to modify gender field,
Initially i have declared gender field as boolean true or false. but now i want it to be changed as list (Male, Female, Other).
class AddExtraFieldsToUser < ActiveRecord::Migration
def change
add_column :users, :phone_number, :string
add_column :users, :date_of_birth, :datetime
add_column :users, :gender, :boolean, default: false
add_column :users, :live_in, :string
add_column :users, :description, :text
end
end
Can i modify as following.... please let me know the correct way...
i thought of doing rails g migration RemovegenderFromUsers gender:boolean
then rake db:migrate followed by creating new one
rails g migration AddGenderToUsers gender:select
user.rb
GENDER_TYPES = ["Male", "Female", "Other"]
html
<%= f.select :gender, User::GENDER_TYPES %>
Is above mentioned process correct or any other way ?
The answer by Ahmad Hussain is correct . List is not a database field type .
You should generate a migration to change the column type :
**change_column :table_name, :column_name, :type**
Select is not a database field type if you want to do it then do it like this
rails g migration AddGenderToUsers gender:integer
In migration file change it to like this:
change_column :users, :gender, :integer, default: 0
For form page do this:
<%= f.select :gender, User::GENDER_TYPES.each_with_index.map { |gender, index| [gender, index] } %>
And in user model you can define function to get gender name to display
def gender_name
GENDER_TYPES[gender]
end

ActiveRecord change column default value (not working)

I've got a companies table in my database with two columns (size and state) that I wish to add default values to. Currently there is no default value and the columns are not set to null: false. I've tried using the update_column and update_column_default methods as outlined in the docs, but neither seems to be doing anything. I'm able to run the migration but there are no changes to the table.
I've tried update_column like so:
class AddDefaultValuesToCompanies < ActiveRecord::Migration
def change
def up
change_column :companies, :state, :string, default: 'MA'
change_column :companies, :size, :string, default: '1-10'
end
def down
change_column :companies, :state, :string, default: nil
change_column :companies, :size, :string, default: nil
end
end
end
I've also tried using update_column_default like so:
class AddDefaultValuesToCompanies < ActiveRecord::Migration
def change
def up
change_column_default(:companies, :state, 'MA')
change_column_default(:companies, :size, '1-10')
end
def down
change_column_default(:companies, :state, nil)
change_column_default(:companies, :size, nil)
end
end
end
What am I missing here?
Your migration will only change the default value for newly created records. I would write a rake task to update the existing values in the DB. Use something like
Companies.where(state: "").map{|c| c.update_attribute(state: "ma")

Rails text field size limit error - text(255)

I have a rails DB and I noticed some save errors when putting a lot of text into text areas.
I checked and for some reason they've been created as either text or varchar but with limits of 255, here's the viewer from a DB client.
I tried the below migration to see if it would change to text with no limit however it's not had any effect:
change_column :investors, :notes, :text
change_column :investors, :has_property_notes, :text
change_column :investors, :jv_partner_notes, :text
Other text fields don't seem to have this problem. Any suggestions would be greatly appreciated!
Thanks
Nick
Use limit nil for stubborn databases. Also you should put this in a new migration.
change_column :investors, :notes, :text, :limit => nil
change_column :investors, :has_property_notes, :text, :limit => nil
change_column :investors, :jv_partner_notes, :text, :limit => nil

Query to define a default value in a database migration

In this SO article I can see how to add a default value to a certain table:
Add a default value to a column through a migration
change_column :shops, :currency_id, :integer, :default => 1
I have another table currencies that has an ID and also a ISO_Name. I want the system to use EUR as default value. But it's possible that this has ID 5 or ID 1 or ...
So my question: How can I define a default value that is based on the result of a query? For example Currency.find_by_iso_code('EUR').id
As you have iso_name field in the currencies, you can achieve it by the following code.
change_column :shops, :currency_id, :integer, :default => Currency.find_by_iso_name('EUR').id
How about:
class SetDefaultCurrencyForShops < ActiveRecord::Migration
def up
currency = Currency.find_by_iso_code('EUR')
if currency
change_column :shops, :currency_id, :integer, :default => currency.id
end
end
end

How to add ":limit => nil" to schema without losing data

I have the following Rails migration:
class AddTextToReference < ActiveRecord::Migration
def change
add_column :references, :source_text, :text
end
end
I now realise that :source_text also needs to be set to :limit => nil.
How can I add :source_text to my schema without losing the data stored in my :source_text column?
I am using PostgreSQL locally and for production on Heroku.
try change_column method
def change
change_column :references, :source_text, :text, :limit => nil
end

Resources