How to save data directly from the migration file in ruby on rails - mysql

In my ROR project, I came across an issue. Previous developer of the project has save an array of data into a single column for a particular field. But now I have to get those data and save it to another separate table with the current table id. Following is my code for get the details from the db:
#ar = AResponse.select("id, selected_barriers");
#ar.each do |p|
p.selected_barriers.each do |barrier|
end
end
What I wanted is I have to save the p.id and barrier to a new table and I have to do it in a migration file. So when the migration runs, it will get all the existing details from AResponse and save it to the new one. Can I do it only with migration file? If so how can I do it?
This is my full code
I tried but its not working: Following is my code
class AnalysisBarriers < ActiveRecord::Migration
def self.up
end
def change
create_table :analysis_barriers do |t|
t.integer :analysis_response_id, :null => false
t.string :barrier
end
#analysis_response = AnalysisResponse.select("id, selected_barriers");
#analysis_response.each do |p|
p.selected_barriers.each do |barrier|
AnalysisBarriers.create(:analysis_response_id => p.id, :barrier => barrier)
end
end
end
def self.down
drop_table :analysis_barriers
end
end
Thanks

Yes you can do data changes after migrations. Whatever data changes you want to do, you can do in change function or up function. Take the following example
class Event < ActiveRecord::Migration
def change
create_table events do |t|
t.datetime :starts_at
t.datetime :ends_at
t.timestamps
end
Event.create(:starts_at => Time.now, :ends_at => Time.now+1)
end
end
Above statement will add data to your table just after its creation.

Related

Migration - How to add a UNIQUE constraint to an already populated table?

I have a technicians table which consists only of name and timestamps columns in production, but now I need to use it as a devise model.
class Technician < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name
end
Using 'rails generate devise technician' led me to a migration with all that is necessary to make my technician model a devise model.
class AddDeviseToTechnicians < ActiveRecord::Migration
def change
change_table(:technicians) do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
end
end
end
But I can't migrate since email is a 'unique: true' column, nor drop my technicians table. What should I do?
I was thinking of setting the email field of each technician as: name_attribute + "#email.com", but what's the best way to do this?
I'm using a MYSQL database.

Rails with mysql cannot create json type column

I have created Rails(3.2) application with mysql(5.7.16) backend. I can't add json type column when creating table but I can able to add json column by new migration. I have used following code in create table migration, What was wrong here ?
class CreateShoppingCartItemSpecialInfos < ActiveRecord::Migration
def change
create_table :shopping_cart_item_special_infos do |t|
t.integer :shopping_cart_checkout_option_id
t.json :special_info
t.timestamps
end
end
end
You should be able to create JSON column type. Probably you run in couple of errors. In case there is a error after migration try first reverting it:
rake db:migrate:down VERSION=<version>
Than you can try like this:
class CreateShoppingCartItemSpecialInfos < ActiveRecord::Migration
def change
create_table :shopping_cart_item_special_infos do |t|
t.integer :shopping_cart_checkout_option_id
t.column :special_info, :json
t.timestamps
end
end
end

Rails rollback, change type and migrate again

I have a requests table;
class CreateRequests < ActiveRecord::Migration
def change
create_table :requests do |t|
t.string :from
t.string :to
t.timestamps null: false
end
end
end
I would like to rollback the database with rake db:rollback STEP = 5
destroy the Request model and create request table with;
t.datetime :from
t.datetime :to
However, I have a migration table to Request model on STEP = 2,
class AddStatusToRequest < ActiveRecord::Migration
def change
add_column :requests, :status, :string, :default => "Pending"
end
end
The problem is, if I destroy Request table and create new Request table with datetime types it creates after STEP = 2 and when I rake db:migrate rails does not add Status column to Request table. How can I overcome this?
You can create a new migration to change the column type using change_column:
command line: rails g migration change_request_to_from_column_types'
new migration:
class ChangeRequestToFromColumnTypes < ActiveRecord::Migration
def change
change_column :requests, :from, :datetime
change_column :requests, :to, :datetime
end
end
It's best not to retroactively change migrations after they've been applied to avoid state conflicts like the one you have. Perhaps the simplest solution is to create a new migration to add the types to the requests table using a guard to do nothing if the column already exists:
class AddStatusToRequestIfNotExists < ActiveRecord::Migration
def change
unless column_exists? :requests, :status
add_column :requests, :status, :string, :default => "Pending"
end
end
end
EDIT
You'll also want to make sure that you can run migrations from scratch, so you might want to update your existing migration to guard against a missing table as follows:
class AddStatusToRequest < ActiveRecord::Migration
def change
unless table_exists? :requests
add_column :requests, :status, :string, :default => "Pending"
end
end
end
Not ideal, but it's probably the safest approach.

Rails active record object save action fails because of a malformed query

I have a problem in Rails in the part of my app that handles landing pages for registration emails. When a user is invited to the app, an Invitation active record instance is created, and user is sent an email with a link containing the id of the invitation (a random string token).
Then, when the link is clicked, I store that token in session, and at one point in the service layer, I update it's 'status' attribute to 'clicked', something like this:
#invitation = Invitation.find_by_id(session[:registration][:invitation_token])
unless #invitation.blank?
session[:registration][:account_details]['referer'] = #invitation.promoter.username
unless #invitation.status == APP_CONFIG['invitation_status']['clicked']
#invitation.status = APP_CONFIG['invitation_status']['clicked']
#invitation.save
end
end
Upon executing the #invitation.save line, I get an active record error:
!! #<ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'invitations.' in 'where clause': UPDATE `invitations` SET `status` = 'clicked', `updated_at` = '2015-11-11 11:07:24' WHERE `invitations`.`` = 'fd05ee5a-e790-48cc-9e7e-d30d3e88919b'>
The Invitation's id column name seems to be ommited from the query for some reason, and I can't figure out why. Not sure what is wrong or what to do.
Invitation migration:
class CreateInvitations < ActiveRecord::Migration
def change
create_table :invitations, id: false do |t|
t.string :id, limit: 36, :auto_increment => false
t.string :promoter_id
t.string :email
t.string :status
t.timestamps
end
end
end
Invitation model:
class Invitation < ActiveRecord::Base
#region Callbacks
before_create :set_uid
#endregion
belongs_to :promoter, class_name: 'User', foreign_key: 'promoter_id'
private
def set_uid
self.id = SecureRandom.uuid unless self.id
end
end
You should mark the id column as the primary_key:
create_table :invitations, id: false do |t|
t.string :id, limit: 36, primary_key: true, auto_increment: false
...
I think that in your case the best way to solve this problem is to drop the table and create it again with correct migration. But if there is data that you don't want to loose, you can also try to do it like this:
class MarkIdAsPrimaryKey < ActiveRecord::Migration
def change
change_column :invitations, :id, :string, limit: 36, primary_key: true, auto_increment: false
end
end
Keep in mind that this migration is not reversible, it remains under your responsibility if you will use it ) Good luck !

Create new ruby model instance with associations

This is perhaps a basic question, but it's currently driving me nuts...Perhaps i'm missing something, since i'm diving so deep in the code, but the question is:
How can I make my object with associations?
I have the following migration scripts:
class CreateConcepts < ActiveRecord::Migration
def self.up
create_table :concepts do |t|
t.integer :language_id, :null => false
t.string :uri
t.integer :isco_code
t.timestamps
end
end
def self.down
drop_table :concepts
end
end
class CreatePrefLabels < ActiveRecord::Migration
def self.up
create_table :pref_labels do |t|
t.integer :language_id
t.integer :concept_id
t.string :value
t.timestamps
end
end
def self.down
drop_table :pref_labels
end
end
class CreateLanguages < ActiveRecord::Migration
def self.up
create_table :languages do |t|
t.string :code
t.timestamps
end
end
def self.down
drop_table :languages
end
end
The classes for these objects look like the following:
class Concept < ActiveRecord::Base
belongs_to :language
has_one :pref_label
validates_uniqueness_of :isco_code
end
class PrefLabel < ActiveRecord::Base
belongs_to :language
belongs_to :concept
validates_uniqueness_of :value
end
class Language < ActiveRecord::Base
has_many :concepts
has_many :pref_labels
validates_uniqueness_of :code
end
So if I remember my Ruby lessons correctly, the following code should be perfectly fine:
concept = Concept.first
concept.pref_label
language = Language.find(:code => "en")
language.pref_labels
language.concepts
So i've written the following line of code. The internals are a bit dark, but I'm 100% sure the hashes generated from the JSON data are correct. Checked this with the debugger:
# This function will retrieve all the top most concepts from the ESCO API in the
# specified language as an array of Ruby objects, each represented by the relevant class.
# If the relevant concept does not exist in the database, the entry is automatically created.
# The function will fall back to English by default if no language is specified
def self.top_concepts(lang = Language.find_by_code('en') || Language.create(:code => 'en'))
concepts = []
json = HTTParty.get "#{Setting[:api]}/getTopmostConcepts?language=#{lang.code}"
collection = JSON.parse json.parsed_response
collection.each do |c|
language = Language.find_by_code c['language'] || Language.create(:code => c['language'])
concept = Concept.create(:language => language, :uri => c['uri']['uri'], :isco_code => c['iscoCode'].to_i)
label = PrefLabel.find_by_concept_id(concept.id) || PrefLabel.create(:language_id => language.id, :concept_id => concept.id, :value => c['prefLabel']['string'])
concept.pref_label= label
concept.save
concepts << concept
end
return concepts
end
The problem I'm having now is that my PrefLabels are :
1) not beeing created all the time
2) are never linked to my concept objects.
What am I missing?
First, I would suggest simplifying this snippet of your code a bit like so:
language = Language.find_or_create_by_code(c['language'])
concept = langauge.concepts.create(:uri => c['uri']['uri'], :isco_code => c['iscoCode'].to_i)
concept.pref_label = PrefLabel.find_or_create_by_concept_id(:concept_id => concept.id, :language_id => language.id, :value => c['prefLabel']['string'])
That's untested so it might not be quite right.
Second, check to see if validation is passing or not. valid? will return false if there is a validation issue of some sort -- for example, if the PrefLabel value isn't unique.
I suspect you need to scope your validations, although that's just a guess of course.