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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:12:21+00:00 2026-06-16T07:12:21+00:00

I’ve found some good information on STI but have not seen examples for what

  • 0

I’ve found some good information on STI but have not seen examples for what I want to do. This is the best example I can think of. I want to be able to track two entities but they don’t need their own tables. I need only distinguish type (STI can do this) but I also need to know if and how one entity is related to another. I’ll use books as an example. Some books are just books but other physical books are collections of multiple books.

table: books

id | title | author_id | book_id
1, 'Fellowship of the Ring, 34, (NULL)      # is a physical book
2, 'The Two Towers', 34, (NULL)             # is a physical book
3, 'American Gods', 1, (NULL)               # is a physical book
4, 'Complete Lord Of the Rings', 34, (NULL)
5, 'Fellowship of the Ring', 34, 4          # is collected within book id 4
6, 'The Two Towers', 34, 4                  # is also collected within book id 4
etc.

So I want to be able to query books, for all books and understand how and if they are related to each other by ‘book_id’

Is this possible in Rails? If so, how is it best implemented? Can I say ‘has_many :books’ in the books model? Are their gotchas or concerns, etc.?

Thank you in advance.

  • 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-16T07:12:23+00:00Added an answer on June 16, 2026 at 7:12 am

    something like this might work for your situation?

    class Book < ActiveRecord::Base
      # all attributes of a book
    end
    
    class SeriesBook < Book
      has_many :books, :class_name => "PhysicalBook", :foreign_key => "parent_id"
    end
    
    class PhysicalBook < Book
      belongs_to :series, :class_name => "SeriesBook", :foreign_key => "parent_id"
    end
    

    then when querying

    # searches both Series and Physical
    Book.where("title LIKE ?", "#{params[:title]}%") 
    # searches only Series
    SeriesBook.where("title LIKE ?", "#{params[:title]}%") 
    

    you might find that you really want your models to be different ?
    Series and Book and not use STI? it would make the queries across both more complex but might make the rest of the application easier to understand

    UPDATE: added belongs_to to the PhysicalBook, class name on the has_many association

    # How would I query a specific collection to see which and how many books it has within it?
    series = SeriesBook.find(0000)
    series.books.length # => how many 
    series.books.each do |book|
      puts book.title
    end
    
    # How would I look at a book and see if it was in a collection and, 
    # if so, the specific ID of the specific collection to which it is associated
    book = PhysicalBook.find(0000)
    puts book.series.title
    puts book.series.id 
    

    The book database table ends up looking something like

    id # primary key
    title
    # other book columns ....
    type # STI
    parent_id # nullable, used by physical book when part of a series, points to books.id on the series book
    

    ALSO: read this – http://rhnh.net/2010/07/02/3-reasons-why-you-should-not-use-single-table-inheritance

    you might not want STI ? data model would look similar to above but without the STI, i.e. Series / Book

    use of foreign_key in has_many and belongs_to can be confusing: read up on that here – http://guides.rubyonrails.org/association_basics.html

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.