Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to force model <=> table connexion ?

Status
Not open for further replies.

metaltree

Technical User
Nov 27, 2006
28
0
0
CA
I'm a beginner in Ruby on Rails.

I need to build a web interface over an already-existing PostgreSQL database.

Thus, when I create models, I don't use migration to create tables.

However, I need to name the models in concordance to Rails rules, that is model name (eg. 'provider') is the singular of table name (eg. 'providers'), otherwise the ORM does not work.

Questions:

Is there a way to force Rails to link a model and a table if the names are quite different? (eg. model='people_list', table='customers')

How to know quickly if model<=>table mapping has proceeded correctly?
 
MetalTree:

Take a look at the following:


I think you are looking for this part here:

<quote>
You can, however, explicitly set the table name or even the dataset used:

class Post < Sequel::Model:)my_posts)
end
# or
:post.set_dataset :my_posts
# or
:post.set_dataset DB[:my_posts].where:)category => 'ruby')
</quote>

Next, to see if it's working quickly, I'd use the rails console:

<commands>
./script/console
w = Word.find(1)
exit
</commands>

So in my example I'm creating an instance of my Word model. Clearly your model name will be different. The console will show you the output of the find command, and you should know if it worked or not.

You can check out for more info on the console.

Let me know if this works. :)

Thanks!
Nathan

Want to learn Chinese?
 
Quick follow up:

The solution above utilized a gem plugin. If you want to avoid that then these two links should help:


(click on 'ActiveRecord::Base' in the left column, then then click on 'set_table_name' in the main page)

In short:

<quote>
You can call set_table_name to specify a custom table name for a particular model.

For example:

class Dog < ActiveRecord::Base
set_table_name 'dog'
end

You can also override the table_name method and return a value.

For example:

class Dog < ActiveRecord::Base
def table_name
'dog'
end
end
</quote>

Thanks!
Nathan

Want to learn Chinese?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top