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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:45:36+00:00 2026-05-31T13:45:36+00:00

I need some help. I’m trying to make a table of posts with a

  • 0

I need some help.

I’m trying to make a table of posts with a self-referencing association. One Post may have some replies (also Post). I include only the main parts. My Post class is:

# == Schema Information
#
# Table name: microposts
#
#  id          :integer         not null, primary key
#  content     :string(255)
#  user_id     :integer
#  created_at  :datetime
#  updated_at  :datetime
#  in_reply_to :integer
#

class Micropost < ActiveRecord::Base

  attr_accessible :content, :in_reply_to
  ...
  belongs_to :user
  # SOMETHING SHOULD BE WRONG HERE ?!?
  has_many :replies, :class_name => 'Micropost',
                     :inverse_of => :replied_post
  belongs_to :replied_post, :class_name => 'Micropost',
                            :foreign_key => "in_reply_to",
                            :inverse_of => :replies

  ...
end

Obviously, I have also a User class that owns posts.

class User < ActiveRecord::Base
  ...
  has_many :microposts, :dependent => :destroy
  ...
end

While I can create posts and its replies as usual, I cannot use self-associations. For instance, in the next rails console session I create a post and a reply to it from another user:

--- 
<b>
1.9.2-p290 :014 > usr1=User.find(1)

  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
 => #<User id: 1, name: "Marcel Massana", email: "xaxaupua@gmail.com", created_at: "2012-03-13 00:07:12", updated_at: "2012-03-13 00:07:13", encrypted_password: "339f5f93bbbd7d9b4fdc58a89aa74797c23ccd5acb154e3987a...", salt: "849c6fbeb17e1a4a7e3ef3153d8c41f4ff3ca5435915f87f985...", admin: true> 

1.9.2-p290 :020 >   usr2=User.find(2)

  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
 => #<User id: 2, name: "Pep Massana", email: "fitxia@gmail.com", created_at: "2012-03-13 00:07:13", updated_at: "2012-03-13 00:07:13", encrypted_password: "00707453381192a4cc7a5714df15da0fa4c2ffa944460bc33d2...", salt: "5af002dc3fad187dac26acee522d44d19d6a2daafcec7fb412e...", admin: true> 

1.9.2-p290 :021 > usr1.microposts.create(:content=>"Hi Pep")

  SQL (0.7ms)  INSERT INTO "microposts" ("content", "created_at", "in_reply_to", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["content", "Hi Pep"], ["created_at", Wed, 14 Mar 2012 18:08:45 UTC +00:00], ["in_reply_to", nil], ["updated_at", Wed, 14 Mar 2012 18:08:45 UTC +00:00], ["user_id", 1]]
 => #<Micropost id: 104, content: "Hi Pep", user_id: 1, created_at: "2012-03-14 18:08:45", updated_at: "2012-03-14 18:08:45", in_reply_to: nil> 

1.9.2-p290 :022 > mp1=Micropost.find(104)

  Micropost Load (0.2ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" = ? ORDER BY microposts.created_at DESC LIMIT 1  [["id", 104]]
 => #<Micropost id: 104, content: "Hi Pep", user_id: 1, created_at: "2012-03-14 18:08:45", updated_at: "2012-03-14 18:08:45", in_reply_to: nil> 

1.9.2-p290 :023 > mp2 = usr2.microposts.create(content: "good to see you Marcel", in_reply_to: 104)

  SQL (0.8ms)  INSERT INTO "microposts" ("content", "created_at", "in_reply_to", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["content", "good to see you Marcel"], ["created_at", Wed, 14 Mar 2012 18:11:24 UTC +00:00], ["in_reply_to", 104], ["updated_at", Wed, 14 Mar 2012 18:11:24 UTC +00:00], ["user_id", 2]]
 => #<Micropost id: 105, content: "good to see you Marcel", user_id: 2, created_at: "2012-03-14 18:11:24", updated_at: "2012-03-14 18:11:24", in_reply_to: 104> 
</b>

--- 

Then I try to use Post self-associations, but one of them don’t work: Post#replied_post seems to work, but Post#replies don’t:

--- 
<b>
1.9.2-p290 :024 > mp2.replied_post

  Micropost Load (0.3ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" = 104 ORDER BY microposts.created_at DESC LIMIT 1

<Micropost id: 104, content: "Hi Pep", user_id: 1, created_at: "2012-03-14 18:08:45", updated_at: "2012-03-14 18:08:45", in_reply_to: nil> 

1.9.2-p290 :025 > mp1.replies

  Micropost Load (0.2ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."micropost_id" = 104 ORDER BY microposts.created_at DESC
SQLite3::SQLException: no such column: microposts.micropost_id: SELECT "microposts".* FROM "microposts"  WHERE "microposts"."micropost_id" = 104 ORDER BY microposts.created_at DESC
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: microposts.micropost_id: SELECT "microposts".* FROM "microposts"  WHERE "microposts"."micropost_id" = 104 ORDER BY microposts.created_at DESC
    from /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
    from /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
    from /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
...
</b>

--- 

Any hint? What’s wrong with my association definitions? I’ve read the documentation ans seems well defined.

TIA

PS: My configuration

About your application's environment
Ruby version              1.9.2 (i686-linux)
RubyGems version          1.8.10
Rack version              1.3
Rails version             3.1.3
JavaScript Runtime        therubyracer (V8)
Active Record version     3.1.3
Action Pack version       3.1.3
Active Resource version   3.1.3
Action Mailer version     3.1.3
Active Support version    3.1.3
Middleware                ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x9c4d494>, Rack::Runtime, Rack::MethodOverride, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::RemoteIp, Rack::Sendfile, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
Application root          /home/marcel/Dropbox/DESENVOLUPAMENT/Rails3Examples/ror_tutorial
Environment               development
Database adapter          sqlite3
Database schema version   20120313233558
  • 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-05-31T13:45:37+00:00Added an answer on May 31, 2026 at 1:45 pm

    I rechecked the whole think and found that I had to add the :foreign_key => "in_reply_to" also in the has_many statement.

    has_many :replies, :class_name => 'Micropost',
                       :foreign_key => "in_reply_to",
                       :inverse_of => :replied_post
    belongs_to :replied_post, :class_name => 'Micropost',
                              :foreign_key => "in_reply_to",
                              :inverse_of => :replies
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help. I have a table with some columns..like name , phone etc...
need some help with a MYSQL call. In one of my tables, I have
need some help with this one. I have some reflectance values for a colour
Need some help to solve this. I have a gridview and inside the gridview
Need some help with a query.. I have three tables. Source id name 1
Need some help with DataFormatString in GridView. I have a Double value that needs
I need some help about WCF and authorization. Currently I have a client which
Need some help here. I have deployed spree (0.70.3) on slicehost (ubuntu, ruby1.8.7, Rails

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.