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 9245133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:11:49+00:00 2026-06-18T09:11:49+00:00

I’m building a page layout application that has spreads and pages. Each spread contains

  • 0

I’m building a page layout application that has spreads and pages. Each spread contains two pages (a left page and a right page).

The database looks like this:

create_table :spreads do |t|
  ...
  t.references :left_page
  t.references :right_page
  ...
end

create_table :pages do |t|
  # No foreign keys
end

Semantically, two pages belong to a spread, so it would make sense to set up the models as follows:

class Spread < ActiveRecord::Base
  has_one :left_page, :class_name => 'Page'
  has_one :right_page, :class_name => 'Page'
end

class Page < ActiveRecord::Base
  belongs_to :spread
end

But, since the foreign keys live in the spreads table, Rails seems to demand the reverse:

class Spread < ActiveRecord::Base
  belongs_to :left_page, :class_name => 'Page'
  belongs_to :right_page, :class_name => 'Page'
end

class Page < ActiveRecord::Base
  has_one :spread
end

Whenever I run into something like this in Rails, it makes me think I’m approaching the problem in the wrong way. Is that the case or do I just live with code that doesn’t quite read clearly?

  • 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-18T09:11:50+00:00Added an answer on June 18, 2026 at 9:11 am

    short answer:

    it depends on your business rules.

    longer answer:

    It is a schema design issue, not really a rails issue…

    Since you already decided that the foreign keys MUST be in the spreads table, it means you had good reasons to do so (right ?). belongs_to only reflects this structure.

    Sure, you could have placed those FKS on the pages table. Functionaly, it (more or less) changes nothing… for now. But how will it work when you implement callbacks? How easy will it be to manipulate your objects ?

    This is an architecture decision, and only you can decide.

    Now, IMHO, it seems that your design is right. This structure holds more information in itself that would the other solution : your spread object will always have 0-1 left page and 0-1 right page; not only the fact that a spread has at most 2 pages is reflected in this structure, but each FK also reflects a quality of the association (left or right page). So i would stick with this solution, even if it “reads weird”.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

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.