Inserting CSV data into postgresql table without model - Ruby on Rails 4 - ruby-on-rails

I have a form which will be uploading a CSV file with table name. Using this table name i have to create a table in Postgresql and then i have move the data in CSV to that table.
As i understood Ruby on Rails works with model(related to a table) to write data. But when i create a table dynamically i should also create a model. But I Have searched on internet for the solution but no success.
So Can i insert the data into the temporary table without model?
Thanks in advance.

Related

Rails postgres sql view missing data when creating via migration

I've been speeding up some statistics reports in my rails(4.2.7, postgres 9.5 database) project via creating sql views.
I wrote all code in PGAdmin, checked all data in views. Then I generated migration in my rails project and copied all written sql code to migration.
When I run migration, all views are being created with randomly missing data.
Example:
create or replace view revenue_by_date as
select sum(reward) as reward_coins, count(ot.id) as amount, (sum(reward)::float / substring(value from '---\s''(\d+)''')::bigint) as reward_dollars,
ot.created_at::date as date
from offerwall_transactions ot
inner join settings on var = 'exchange_rate'
where ot.reward > 1
group by ot.created_at::date, settings.value;
I get reward_dollars data missing.
However, when creating via PGA data is ok
Settings table structure:
Offerwall transactions table structure:
I have same problems with other views. Some views are created with no data at all.
Where is a mistake?

How to update tables to match updated gem

I am using the active-record-reputation-system gem, and would like to update it to v 2.0.0.
The gem creates three tables, RS_Evaluations, RS_Reputation_Messages, and RS_Reputations. These three tables have been renamed for v 2.0.0, but they do not get updated in my database by simply bundle installing the v2.0.0 gem.
How can I go about updating these tables? Should I:
1) Update just the table names with the content of the columns intact? (To the best of my knowledge, the column names are unchanged with the update). If so, how can I do this?
2) Drop the older tables and create new correct tables? This is not ideal, but won't kill me, as I don't have a huge amount of data in the existing tables. Can I simply delete the older tables via my database viewer (ie. PGAdminIII)? Or do I need to migrate a file that drops the tables?
Input would be much appreciated!
UPDATE
The migration guide has a line about
Also, you need to update your database data as follow:
UPDATE rs_reputation_messages SET sender_type = 'ReputationSystem::Evaluation' WHERE sender_type = 'RSEvaluation'
How would I go about doing this? I am only familiar with updating the database through migration files. Is this an SQL call made directly to the database?

Best way to import data into my new rails app?

Hi I have a sqlite3 database full of data from my previous version of the web app which WASN'T written in rails. Im now rewriting the web app in rails from scratch. But Id like to use the data from my previous app in the new rails app. Whats the best way to accomplish this?
This is what Ive tried so far and it didn't work very well:
1) I created a new rails app
2) replaced my sqlite3 database in the new app with the sqlite3 database from the old app
3) Created a model with the same schema as the old DB.
4) changed the databse.yml file with the updated DB file details
5) un my model added the "establish_connection" method
6) I could thus get it to show me all the details of the old database in my browser #"index.html"
7) However, I ran into problems when wanting to insert/edit records into the DB. Since the DB did not have a primary key column for each row, it didnt work.
8) So I tried to do a migration to add a column with a primary key . It didnt work
9) suddenly a new development.sqlite3 database had shown up in the app and it was trying to add a primary key to the NEW DB.
10) So I just deleted the new DB that had popped up and after that the app wasn't working
11) Now I want to start from scratch and so my question:
"Whats the best way to import data from a previous non-rails app (in a sqlite3 DB format) into a new rails app"
You want to be working with 2 databases, the old and the new. So, your database.yml needs to have 2 connections in it. One should be your normal connection called development or production and the other should be called legacy. Fill out their different connection information. By default, your ActiveRecord models will pull from the one not called legacy.
Create a model called LegacyBase. In the model put establish_connection "legacy". Now create a directory at app/models/legacy. Put all of the models represent data from the old DB inside this directory. All of those models should extend from LegacyBase. This will mean they're all reading from the old DB.
Create your new models. Do you want them to have a primary key column? If not, see this answer. Create an ActiveRecord database table with no :id column?. They will by default have an id column, though and I recommend leaving it that way.
For each of the legacy models, write a method called to_model. In this method, write the code that creates a new object and populate it with the old data and save it. Something like this:
class OldUser < LegacyBase
establish_connection "legacy"
def to_model
User.create!(self.attributes)
end
end
You can do any logic that needs to happen to make the old data fit your new app. Call this method on all of the old records like OldUser.all.map(&:to_model).
Do this for all of the tables that you want to move over.

Empty database in rails

What is the best way to test for an empty database in rails? I generated a model chardata and the controller, Chardatum. I want to loop through the database and extract a certain column, but I need a way to test if the database is empty first.
If you are fetching some Chardata allready you could do:
#chardata.any?
It depends on two things.
What kind of database are you using?
And by empty, do you mean the tables have not been created yet? or just that data has not been inserted yet in to the tables?
To see if the chardatas table has no rows before proceeding:
Chardata.count.zero?
Assuming you're using MySQL, you can use the following command to see if tables have been created already in your database
Chardata.count_by_sql "SELECT COUNT(DISTINCT 'table_name') FROM 'information_schema'.'columns' WHERE 'table_schema' = 'your_db_name'".zero?
You can also check if the table has been created with CharDatum.table_exists?.

Adding id column and populating it to an existing table in Rails?

I have an existing table that I'd like to use for a Rails application.
It's a simple table with only 4 columns. However it does not yet have id column. And also new data will be added periodically.
I am trying to find a way to add the id column and populate it.
I guess I have two options, but being a noob I am sure there are better ways.
Option 1: I can add the id column and populate it when I parse raw data into CSV files, and then import it to the Rails database. In this case, when I parse the data into CSV files, I need to figure out how to find the last used unique id is.
Option 2: Parse raw data into CSV files, then import to the Rails database. Then my rails application will populate the id column for the new data entries.
If Rails has a built in method or GEM that can populate the id fields for the new entries, that would be great. In that case I will go with the Option 2.
If not, I think it's easier to go with the Option 1.
So I guess the question becomes this: Can Rails automatically populate the id column of entries with blank id field?
Thanks!
Either option should work because the underlying database will automatically handle the id column since it's the primary key. So create the table using a migration, then parse, import, and add the CSV data to your database via which ever method sits best.

Resources