How can I reference data from an already existing table? - ruby-on-rails

I have connected my app to a MS SQL database, but I wanted to know how I can start referencing the data that was already in the database (but in another table). Do I have to generate anything in my app / resources etc.?

As Dharam said, you need to genereate Active Record model. Rails using MVC architecture, if you need to learn first about MVC, go to Rails getting started.
If you already have table, with data and structure, you don't need to generate it, you can generate ActiveRecord model without migration with this command.
rails g model ModelName --migration=false

Related

Is it possible to add a database filed to the existing database table without rails g migration in rails

I am new to Ruby on Rails, so I have some confusion about rails migration.
I want to add a field on my user table, according to rails documentation is only possible by using
rails generate migration add_field_name_to_table_name field_name: type
Question
Is it possible to add fields to the existing database table without rails g migration in rails? Thanks...
The bigger question is why you need to do that. I mean to imply that whatever makes you think that you need to do this is wrong (I am pretty confident about it, assuming you are a new rails user). It is like you would leave your plane without a reason for a long-distance flight to travel by foot.
Answer:
You can do that by:
logging in to Postgres manually like rails db
then creating table manually the SQL way
then adding the table manually with correct columns in your schema.rb file
then creating a model manually
You still will have to repeat this every time this project is cloned anywhere else with no window for mistake. With migration, on the other hand, you will just need to do rails db:migrate
Happy Coding.

How to write test for Ruby on Rails API that is built on an existing database?

I am trying to build a read-only Rails API that uses another Rails application's DB as its database.
So far I configured the read-only API's database.yml to point to the existing databases, generated models via rails g model but did not run the migration since I already have the corresponding tables in the existing databases.
I wanted to follow TDD approach and started to write tests but my problem is I cannot create instances of models since I did not create the table for that model.
Example Case
I generated a model named project which exists as a table in the existing DB that I am going to use to read data, but since I did not run the migration for that model it does not exist in the current API's DB.
So, I just dumped the schema.rb from the existing DB, copied it as my read-only API's db/schema.rb and run rake db:schema:load when RAILS_ENV=test.
I wonder if there's a better way to do this? Is my way of writing a read-only API like this is correct? I am open to any suggestions for this topic.
Cheers.

Integrate new rails app with existing external MS SQL Server database

I need to create a new rails application that will use an existing external Microsoft SQL server database as its primary database. This DB is running on an external server that i'll need to connect to. Here where i work there's a separate DB Admins section so im not sure what steps i need to perform. I think the easy part is connect to the server and set the app to work with that DB. But i get confused after that.
Should i create a db dump and migration?
Should i add models to represent existing database structure?
In case i need to create new models and tables in the db...
Should i generate its models from the rails generator and run migrations on the db or contact the DB admins to create the tables, and then specify manually the models inside the app? <- this really confuses me
Thank you in advance and sorry for my english! :)
EDIT!! -> The Rails Way
I've found another great way to integrate multiple database data... You can define a model and specify in which database it's located (so db connection is never closed)... This is definitely The Rails Way and should be followed...
Here's a nice tut:
Multiple Databases in Rails
TL;DR
Define your secondary_db in database.yml
Define your model and specify its database and table name
Class ExternalDatabaseModel < ActiveRecord::Base
establish_connection(:secondary_db)
self.table_name = "the_table"
self.primary_key = 'someWeirdIDColumn'
end
Then use as any other rails model, specify relationships and more :)
Hope it helps to somebody!
I would suggest you take a look at tiny tds gem: https://github.com/rails-sqlserver/tiny_tds. It's really use to config and use... you can use it to connect on your external sql server db and make the queries you want. Don't know which type of system you're working, but if you're constantly retrieving data, making external requests may get your system slower. Depending on your application, you could consider synchronizing the data, making a local database redundant. Hope it helps you, good luck!
EDIT: about the models part: if you're going to retrieve the data from that sql server every time you need, than you don't need to store it locally. But if you're doing that redundancy, you should create the models with the attributes you'll need, than when you retrieve from the server, you save it normally, just attributing the values to your model and calling save! method.

What internal tables are automatically created by Rails?

I'm working on blacklisting these for the data analysis tool Metabase.
I can't find any lists anywhere that specify which tables the framework creates, but I want to make sure I get db migration / ActiveRecord ones and other internal ones created by the framework that a user likely wouldn't be interested in looking at.
The only table that Rails (or rather ActiveRecord) creates regardless of application models is schema_migrations.
By default rails does not create any table when you create a new app.
But when you add a migration for create table that time rails create a table schema_migrations and that is used to save version of the migrations.
This table will create with first table when run rake db:migrate

Rails: best way to start an app with multiples models

I want to create a Rails app but I have a question before start it.
I have defined a database model in paper (about 15 tables) and I don't know which is the best way to start the application:
Create the tables on database with my database client and after that in console do:
rake db:schema:dump
with this I will obtain the shema.rb and after that do a :
rake db:migrate
or
In console, one to one, create the models, edit them with an editor and do:
rake db:migrate
I think the first one is more quickly but the second I think is better from the point of view of rails.
I am bit confused about that, can anyone help me with this question?
Rails mean the only sure way to create a database for a new application - migration.
You can create a single migration for all of your application but such migration is not easy to roll back.
Therefore wise to create a migration for each table.
Normal situation when you have 100,500 migrations in the end. For that you get your application is ready for deploy to combat server.
Permitted the creation of migration through model and scaffold or independently.
Why not use a generator?
rails g model User first_name last_name age:integer email user_name
You don't need to specify type if it's string. This will generate model class and migration.
I think there may be varying viewpoints on this issue.
Also, it depends if you're using the rails built-in sqlite database.
However, assuming that you do use the default rails db configurations,
I personally prefer the second choice.
Later, you might want to change some parts in your tables.
If you follow the rails way of doing so, this process will be very easy and less confusing.
Also, you can always add custom ruby scripts for pre-loading with data.
Of course, other people might find the other way easier.

Resources