SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Packetloss's Avatar
    Join Date
    Aug 2003
    Location
    Behind You
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Model & Table relationship

    Hi,

    I'm just starting out with RoR, so bare with me.

    When I create a new model, controller, etc, how does it know which table to use in the DB? When I create recipe, it magically knows to use the table recipes. What if I want to use a different name? How would I change that?

    Thanks!
    My links: [ Blog ] - [ deviantArt ]

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    class YourModel < ActiveRecord::Base
       set_table_name "your_table_name"
    end

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Packetloss
    Hi,

    I'm just starting out with RoR, so bare with me.

    When I create a new model, controller, etc, how does it know which table to use in the DB? When I create recipe, it magically knows to use the table recipes. What if I want to use a different name?
    You shouldn't use a different name between models and tables other than pluralization differences, unless you have a legacy system you need to connect to.

    Rails uses the model name = table name convention for a reason. Those reading your code for the first time, assuming they understand Rails principles, will automatically know what database table to look at just based on the model name.
    Quote Originally Posted by Packetloss
    How would I change that?
    See fenrir2's post above.

  4. #4
    SitePoint Zealot Packetloss's Avatar
    Join Date
    Aug 2003
    Location
    Behind You
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info guys.

    I was looking into this because I build sites in languages other than English at times and would want to use different table names
    My links: [ Blog ] - [ deviantArt ]

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should check out plugins to translate your content. I'm currently writing a CMS and I'm using the Globalize plugin to translate everything from system phrases to content

    http://globalize.diluvia.net/wiki


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •