Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:36:32+00:00 2026-06-10T14:36:32+00:00

We have a simple item and order model. orders(url) items(title, orders_count) Where url can

  • 0

We have a simple item and order model.

orders(url)
items(title, orders_count)

Where “url” can be any URL from the web for e.g. http://amzn.to/aCKiXO. What we would like to do is that if the user enters “/items/7” for the “url” then have it behave like a foreign key. So something like:

class Order < ActiveRecord::Base
  belongs_to :item, :foreign_key => :url, :regex => /items/n, 
                    :counter_cache => true
end

class Item < ActiveRecord::Base
  has_many :orders, :foreign_key => :url, :regex => /items/n, 
                    :dependent => :destroy
end

Is this possible? We’re on Rails 2.3.8, Ruby 1.9.3 and on Postgresql 9.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:36:33+00:00Added an answer on June 10, 2026 at 2:36 pm

    No, it isn’t possible to add a foreign key constraint that does a regular expression match – or anything except simple equality. See the PostgreSQL documentation on constraints.

    What you can do is any of:

    • Write a constraint trigger in PL/PgSQL to enforce the constraint you want;

    • Split out the part you want to add a constraint on using a regexp in the application, and define a foreign key constraint on a column containing only that part; or

    • Use a BEFORE INSERT OR UPDATE ... FOR EACH ROW trigger to split the part of interest out in PL/PgSQL when the row is inserted and add the part of interest to a foreign key column that contains only that part. The app doesn’t need to know about the duplication since the DB is taking care of it behind the scenes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple unordered list with list items as menu item i created
I have a list of items, where each item is a simple class containing
Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
I have multiple ordered lists. Unfortunately, the order of the items isn't a simple
Have simple Order - Item app. No cart. Need help with joined scopes for
I have a simple FAQ page in which each item has the following HTML
I have a simple producer/consumer scenario, where there is only ever a single item
I have a simple list like this: <ul id=cyclelist> <li>Item 1</li> <li>Item 2</li> <li>Item
I have some simple markup: <table> <tr> <td>Menu Item</td> </tr> </table> <div> <table> <tr>
Hopefully this is simple: I have a relatively long list where each list item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.