SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Rails Datatabase Question - Lookup Tables?

    I'm very new to RoR and am trying to build a pretty simple application. I think that most of the admin section can be created using the scaffold function (method, or whatever) but I have a question about the database.

    When I was planning the database I was intending on building the software with PHP and MySQL. I have a table of listings, and a table of features - and was planning for a lookup table so I could reference listings and features. One to Many.

    Do I still add the lookup tables to the database, and what's the correct format so RoR will recognize them, if needed, to build the scaffold?

    Again, I'm very new to RoR and Ruby, so any help or links to tutorials would be appreciated. I have a couple tutorials and the Pragmatic book on RoR but haven't found anything about lookups.

    Thanks!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rails scaffold doesn't support associations (many-to-many/has-many/etc).

    Oops, one to many

    model listing:
    Code:
    has_many :features
    model feature:
    Code:
    belongs_to :listing
    table features:
    Code:
    id integer
    ....
    listing_id integer
    There is a lot of information about this on page 255 and in the rails wiki:
    http://wiki.rubyonrails.org/rails

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Fenrir is saying, you don't need a lookup table, just a column. (unless you really want many to many)

    The scaffold feature is so easy, everyone wants it to do everything when they get started. I still want it to do everything, but it just doesn't.

    You can add those associations really easily, though. Fenrir covered the model, I'll try to overview the controller. Something like:

    Code:
    def new
      @listing = Listing.new
      @features = Feature.find(:all)
    end
    You could change that find so it only listed some features. That @features array will be available in the new.rhtml view so you can make a checkbox list or whatever with it.

    Create needs to be changed to get the features added to the listings.
    Code:
     def create
        @listing = Listing.new(@params[:listing])
        if @listing.save
          @listing.features= Feature.find(@params[:array_of_feature_ids]) 
          redirect_to :action => 'list'
        else
          render_action 'new'
        end
      end
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  4. #4
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Okay, I see what' you're saying. So as long as there is just a one-to-many relationship (i.e. the listing has a lot of features, photos, whatever) then I just need one field in the database for the ids?

    Still not quite sure how that will work - will it just put a comma separated list of the ids in the field?

    I'm still working through this - but I've got the basic application up and running (without these associations). The main tables in the database are working, and after going through several Rails tutorials I'm feeling more confident about it.

    Still a little confused about this though... but I'm trying to work my way through it. My application is more complex than the example I gave above, but all the associations are one-to-many (no many-to-many) so hopefully it'll be easier than I think to get it up and running.

    I'm new to the Ruby language too - so I'm on a double learning curve. I'm thinking of just making this thing w/ PHP and MySQL

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might benefit from looking at the rails api. It takes a little getting used to, but is very helpful.

    Relative to your questions, you may want to look at the documentation for find and has_many. In the has_many docs, "collection" represents the things you can do with listing.features.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I got it - I have several one-many relationships (photos for instance) but the features will be many-many. I've found tutorials for both, so I'm working through them now and hopefully in a while I'll have something workable. I'm also looking at the API - thanks for the link.


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
  •