How to update tables to match updated gem - ruby-on-rails

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?

Related

Create Ruby on Rails project from an sql file

I have a previously separately managed sql file containing rather simple but large database. Would there be a way to import this sql file and generate ruby code as models using this data as a starting point for my future development?
Thank you for your help!
Yes!
It will take some work!
And you'll need to post a WHOLE HELL OF A LOT more detail to get more than that. ;-)
Taking a stab:
Rails can use legacy databases with a lot of effort manually specifying foreign key columns, table names, etc. It can be done. My suggestion, though, would be to convert the data in-place in whatever database you have by using a lot of ALTER TABLE RENAME... work and same for columns to make the old DB conform to Rails' convetions (primary key == 'id', table name is plural underscore'd version of model name, all that) before doing the import, and then you can just use plain vanilla ActiveRecord and all will be easy.

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?.

Using ar-extensions' import: synchronise doesn't work

I am using AR-Extensions to import a large number of objects to db, but synching them back from DB just isn't working.
MY code:
posts = [Post.new(:name=>"kuku1"), Post.new(:name=>"kuku2"), ...]
Post.import posts, :synchronize=>posts
posts are submitted to db, and each one is allocated with primary key (id) automatically. But when afterwards checking the objects in posts array, I see that they don't have id field, and new_record flag is still true.
I also tried adding :reload=>true, but that doesn't help as well.
Any idea why synch doesn't work?
This is not possible right now with new records. As of ar-extensions 0.9.3 this will not work when synchronizing new records as synchronizing expects the records you're sync'ing to already exist. It uses the primary key under the covers to determine what to load (but with new records the primary key is nil). This limitation* also exists in activerecord-import 0.2.5. If you can synchronize on other conditions I'd be happy to release a new gem allowing conditions to be passed in. For Rails 3.x you need to use activerecord-import though (it replaces ar-extensions). Please create ticket/issue on github: https://github.com/zdennis/activerecord-import/issues
For Rails 2.x you still want to use ar-extensions, and I'd likely backport the activerecord-import update and push out a new gem as well. If you'd like this functionality here please create a ticket/issue on github: https://github.com/zdennis/ar-extensions/
Patches are welcome as well.
*The limitation here is a database constraint, as its impossible to get the ids of all newly created records after a single insert/import without doing something strange like table locking, which I don't think is a good solution to that problem. If anyone has ideas I'm all ears.
UPDATE
activerecord-import 0.2.6 and ar-extensions 0.9.4 have been released and includes support for specifying the fields you want to synchronize on. Those fields should be unique. See http://www.continuousthinking.com/2011/4/6/activerecord-import-0-2-6-and-ar-extensions-0-9-4

Why doesn't MyModel.all.each{|m| m.destroy} work as expected?

I want to clear a table in my railsapp , without dropping the database and migrating...
MyModel.all.each{|m| m.destroy}
I would expect this code to delete every record in the my_model table, but this is not happening...
using Rails 2.3.4 + MySQL 5.1
EDIT:
the issue was based on the plugin better_nested_set which didn't allow me to delete the entries in that order
MyModel.delete_all worked on the other hand , maybe because it executes truncate on the database (?)
Use MyModel.destroy_all to delete all the records for your model.

Resources