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

  • Home
  • SEARCH
  • 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 8671581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:59:04+00:00 2026-06-12T18:59:04+00:00

I define a Post class which can link or be linked to multiple posts.

  • 0

I define a Post class which can link or be linked to multiple posts. To do this I added a PostLink class which specifies post_to and post_from.

I generated the PostLink class by rails g model post_link from_post:integer to_post:integer and of course rake db:migrate, and added

belongs_to :from_post, :class_name => 'Post' 
belongs_to :to_post, :class_name => 'Post'

to the class.

And I also have has_many :post_links in my Post class.

I ran rails console and Post.new.post_links and got nil printed out, which is expected. However after I save a Post using

p = Post.new
p.save

and then run p.post_links, it prints out the following error message:

SQLite3::SQLException: no such column: post_links.post_id: SELECT "post_links".* 
FROM "post_links"  WHERE "post_links"."post_id" = 1

So anybody know why after saving it to the database post_link can not be accessed?

  • 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-12T18:59:06+00:00Added an answer on June 12, 2026 at 6:59 pm

    The has_many :post_links association in Post throws an error because it assumes the foreign key in post_links is post_id by default. Since you are using from_post_id and to_post_id, you will have to find a way to group the post_links for “from” posts and “to” posts to get the total set of post_links for a post.

    One approach could be to define two associations on Post and an additional method to add the sets together:

    class Post < ActiveRecord::Base
    
      has_many :from_post_links, :class_name => 'PostLink', :foreign_key => :from_post_id
      has_many :to_post_links, :class_name => 'PostLink', :foreign_key => :to_post_id'
    
      def post_links
        from_post_links + to_post_links
      end
    
    end
    

    As another option, you could provide special sql to the association to group the sets in a single query:

    class Post < ActiveRecord::Base
    
      has_many :post_links, :finder_sql => Proc.new {
        %Q{
          SELECT *
          FROM post_links pl
          WHERE pl.to_post_id = #{id} 
          OR pl.from_post_id = #{id}
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering how $link can be undefined. It is a global inside class
I'm trying to create a generic LINQ-TO-SQL repository based on this post which basically
I'm trying to create a POST request using angular.js to this Django view. class
This blog post of the ADO.NET team shows in an example how to define
I have an abstract class which defines a number of classes which can be
I will post my code below as a quick note I define both of
I have a wcf service defined like this: [OperationContract] [WebInvoke(Method = POST, ResponseFormat =
I'm reading this post . It defines a simple array: var myArray = [1,
I haven't found similiar post so I'm asking this. Let's say I defined somewhere
I'm compiling using Code::Blocks on Windows 7 using the MinGW compiler (which I can

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.