How to create a Rails 3 model supported by a database table without its attributes dynamically generated by ActiveRecord?

I am trying to create an API for a third-party service, and I have been granted access to a replicated database with all the data I need. Unfortunately, there is a restriction on what privileges were granted for use in MySQL, as it does not disclose your own database design.

The database is configured to show specific database tables as views, and SELECT privileges are granted in these views.

I have a model supported by a table that does not follow typical Rails table naming conventions, which are defined as follows:

class MyAPIModule::City < ActiveRecord::Base
    set_table_name "VIEW_CITY"
end

Any request in which ActiveModel tries to dynamically create attributes for the model does not work, for example:

MyAPIModule::City.find(1)

Mistake:

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 privileges do not allow ActiveRecord to run a "SHOW CREATE TABLE" query to collect column information and create model attributes.

Is there a way to hard-code a table schema that prevents ActiveRecord from querying the database to build the table with "SHOW CREATE TABLE"?

EDIT:

In connection with my answer to sameera207:

class MyAPIModule::City
    include ActiveModel::Validations
    include ActiveModel::Conversion
    include ActiveModel::SerializerSupport
    extend  ActiveModel::Naming

    ...
end
+4
source share
3 answers

, , , , , ActiveRecord...

DataMapper - , , , Rails .

0

, , sql.

,

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
+1

:

default_scope select("column1, column2, column3")
0

All Articles