SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

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

    Enforcing uniqueness constraints

    Okay, let's assume I have two database tables, pages and sections. Each page belongs to a section. Each section cannot have two pages with the same filename. Let's assume this is my page model class:
    Code:
    class Page < ActiveRecord::Base
      validates_uniqueness_of 	:filename
    
      validates_presence_of 	:page_title
      validates_presence_of		:page_content
    end
    Now, my "validates_uniqueness_of" constraint somewhat works, but for example I can only have one page named "index" regardless of how many sections I have. How would I enforce the rule that the combination of section_id and filename must be unique, rather than just one or the other?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if there is a way to easily to do this using Rails' quick and easy helpers - as good and useful as they are, any form of medium to complex validation is probably best done using the validation methods:

    Code:
    class Page < ActiveRecord::Base
      validates_presence_of      :page_title
      validates_presence_of      :page_content
    
      def validate
        # check for an existing filename/section_id combo here
      end
    end

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Scrap that, what you want is the :scope option.

    Code:
      validates_uniqueness_of      :filename, :scope => 'section_id'
    Just found this in the API docs. Don't you just love rails

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Very cool Luke . I'm off to do some JSP work now but I'll revisit this later tonight

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did this work for you in the end Vinnie?

  6. #6
    ☆★☆★ 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 Luke Redpath
    Did this work for you in the end Vinnie?
    I haven't been able to get back to it unfortunately (I've been working on the project, but other parts of it) . I'm hoping this weekend will give me more time to work on this.

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Coming back to this now, and it's definitely working! Thanks for the help Luke


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
  •