I have created three tables and its attributes in upper case.One of my tables is:USER and its attributes are:NAME,ADDRESS.I am able to insert the data from the mysql server and the data is inserted successfully.My problem is that through rails console,i am unable to access the tables.Please specify what to do and notify me where have done mistake.
Thank you.
According to rails naming convention,
the table names should be the plural to the model names.
Example: User model table name should be users
As you said, you have a dump sql file,
first you should run db:schema:dump to dump the schema.
Next, if the table name of the dumped file is not according to the naming convention of rails, change the table_name using
self.table_name = "USER"
so, In your model,
class User < ApplicationRecord
self.table_name = "USER"
end
Now, User.all runs the command,
SELECT USER.* FROM USER
Related
So I am working on a project for a client that wants to use an old mysql database for a new site.
I cannot change the names of the tables in the database because another app uses the same database. I am using Rails to code the new site.
The current name of the tables in the database don't match Rails naming conventions. Is there a way to associate a model "user.rb" with the table name "tbl_user"?
(Also, I tried creating a .rb file named "tbl_user" but the table name would need to be "tbl_users").
Thanks for any help you can give!
You can do this as following:
In your model file user.rb, you put this line:
class User < ActiveRecord::Base
self.table_name 'tbl_user'
end
And your model User will map to your table tbl_user
I am now developoing a rails project, but I am using an existing database of MySQL. Well, I have already connected the project with MySQL database, and in former times, I used to created models together with the database migration.
Like, when I create a new Class for the Model part, I used "bundle exec rake db:migration" to create a new database table, and the activerecords are connected.
But now the sequence is a little bit different, I had my database tables first, and then I wanted to query the records using a newly-built class on rails. For example, I had a table of students in MySQL database, and then I wanted to query the records of all the student by creating the new class Student, and create a new search method in the Student Controller.
But now I have not even a clue, because this is new to me, anyone can come for a little help??? Thanks a lot!
All you have to do is setting table name.
class MyFancyModel < ActiveRecord::Base
self.table_name = "myFancy_table_name_1234-5"
end
I have a brand new Rails app that I want to hook into an existing MySQL database to do some reading and writing. I've already edited my database.yml file to connect to the new db. rails c and rails s don't throw errors which lead me to believe that the connection is valid.
I haven't created any models or migrations yet. I was wondering if there was an easy way to get the models I need into my Rails project.
I'm able to connect to the db with Sequel Pro if I need to export a backup or a schema. Or do I need to generate models and copy all of the column types and everything manually?
Thanks for your help.
ActiveRecord will detect the column names for you! You don't need to create any migrations, but you do have to make the models.
When you make an active record model, active record will deduce the table name that you're connecting to by pluralizing the class name.
So:
# app/models/book.rb
class Book < ActiveRecord::Base
end
Will try to find a table called "books". You can then instantiate an instance of Book, and you'll find it has getters/setters for your field names.
If your tables don't follow this naming convention, you can also define your table names manually:
class Mouse < ActiveRecord::Base
self.table_name = "mice"
end
http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html
Start by creating empty model files with the following structure, for an orders table:
class Order < ActiveRecord::Base
end
With just that much in place, you can get all the active record magic on the orders table. You could do the following from the console:
> Order.count
=> # Shows the number of rows in orders table
> Order.first
=> # Return the first row from the table
> Order.where(...)
=> # Return selected rows from the table meeting the specified criteria.
See Active Record Query Interface for more active record features that you get by subclassing from ActiveRecord::Base.
I'm attempting to create an API for a third party service and I've been given access to a replicated database with all of the data I need. Unfortunately, there is a limitation on what privileges have been granted to use in MySQL as to not expose proprietary database design.
The database is set up to expose certain database tables as views, and SELECT privileges are granted on those views.
I have some model backed by a table that doesn't adhere to typical Rails table-naming conventions, defined as follows:
class MyAPIModule::City < ActiveRecord::Base
set_table_name "VIEW_CITY"
end
Any query where ActiveModel tries to dynamically build attributes for the model fails, example:
MyAPIModule::City.find(1)
Failure:
Mysql2::Error: SHOW VIEW command denied to user 'theuser'#'thehost' for table 'VIEW_CITY': SHOW CREATE TABLE `VIEW_CITY`
ActiveRecord::StatementInvalid: Mysql2::Error: SHOW VIEW command denied to user 'theuser'#'thehost' for table 'VIEW_CITY': SHOW CREATE TABLE `VIEW_CITY`
The problem is that the priveleges don't allow ActiveRecord to run the "SHOW CREATE TABLE" query to gather column information and build the model attributes.
Is there a way to hard-code the table schema that would prevent ActiveRecord from needing to query the database for the table construction with "SHOW CREATE TABLE"?
EDIT:
In conjunction with my reply to sameera207:
class MyAPIModule::City
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::SerializerSupport
extend ActiveModel::Naming
...
end
I'm not sure is there a way to skip the column constriction from the data base, however one work around would be directly execute your sql command.
It will require some coding manually,
class MyAPIModule::City < ActiveRecord::Base
set_table_name "VIEW_CITY"
def find(id)
obj = ActiveRecord::Base.connection().execute("select * from your table")
#above will return an array, so you will have to create your object
obj
end
end
You could try defining the select scope manually:
default_scope select("column1, column2, column3")
I'm going to answer my own question because I found a very nice solution that allows you to do everything I was trying to accomplish, and provides similar query methods that ActiveRecord does...
DataMapper is a gem that supports explicit database-field mapping, intended for use with legacy databases, or databases where Rails doesn't have control over the schema.
In my rails application, all models to be renamed with out losing data. The database in use is mySQl. Please help me in this regard
You can rename the tables names from mysql console using following syntax:
RENAME TABLE old_table TO new_table
If you want to keep the old table names but a new model class name use set_table_name method:
Lets say your model was called Book and new you renamed it to NewBook. If you want to continue to use the books table :
class NewBook < ActiveRecord::Base
set_table_name 'books'
end
To rename the model use the IDE like RubyMine. They have very good code refractoring tools.