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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:53:46+00:00 2026-05-25T20:53:46+00:00

I am developing a mentions system similar to the one in twitter. So I

  • 0

I am developing a mentions system similar to the one in twitter. So I am dealing with three classes in this problem: Post, User and Mention. The mention object has a mentioned_id field (the mentioned user) and a post_id ( the post in which it happens).
I found some weird problems while writing tests, so I opened a console session, and this is what happens:
I create two users, two posts, and two mentions. If you notice, when I create the second mention (@mention2) the id of @mentioned_id is not the correct user’s id:

ruby-1.9.2-p180 :001 > @user1 = Factory(:user, :email => Factory.next(:email))

 => #<User id: 1, name: "Fulano", email: "person_number1@example.com", created_at: "2011-09-11 15:38:11", updated_at: "2011-09-11 15:38:11", encrypted_password: "ede4e8cc0440b8bd9fdc059e8496154715b6592690157aac618...", salt: "4bfae8fecee1629b7c290c2d5af5bd3bbeb38c4ce76546d7176...", admin: false> 

ruby-1.9.2-p180 :002 > @user2 = Factory(:user, :email => Factory.next(:email))

 => #<User id: 2, name: "Fulano", email: "person_number2@example.com", created_at: "2011-09-11 15:39:06", updated_at: "2011-09-11 15:39:06", encrypted_password: "ddda10f48575f63724e1db3d3cf684f2c60380325236311e15d...", salt: "911121b8942dc57116dfd24383d96874c750bc5a21fa210288b...", admin: false> 

ruby-1.9.2-p180 :003 > @post1 = Factory(:post, :user => @user1)

 => #<post id: 1, content: "Foo bar", user_id: 1, created_at: "2011-09-11 15:39:57", updated_at: "2011-09-11 15:39:57"> 

ruby-1.9.2-p180 :004 > @post2 = Factory(:post, :user => @user2)

 => #<post id: 2, content: "Foo bar", user_id: 2, created_at: "2011-09-11 15:40:37", updated_at: "2011-09-11 15:40:37"> 

ruby-1.9.2-p180 :005 > @mention1 = @post2.mentions.create :mentioned_id => @user1

 => #<Mention id: 1, post_id: 2, mentioned_id: 1, created_at: "2011-09-11 15:41:33", updated_at: "2011-09-11 15:41:33"> 

ruby-1.9.2-p180 :007 > @mention2 = @post1.mentions.create :mentioned_id => @user2

 => #<Mention id: 2, post_id: 1, mentioned_id: 1, created_at: "2011-09-11 15:42:16", updated_at: "2011-09-11 15:42:16"> 

Strangely enough, if I use @user2.id instead of just @user2 that last line, everything works fine:

ruby-1.9.2-p180 :008 > @mention2 = @post1.mentions.create :mentioned_id => @user2.id


=> #<Mention id: 3, post_id: 1, mentioned_id: 2, created_at: "2011-09-11 15:45:16", updated_at: "2011-09-11 15:45:16"> 

Can anyone please tell me what’s happening? I thought rails “magic” took care of getting the id when referencing an object. And why would it work with the first user but not with the second??

This is the code for my mention class:

class Mention < ActiveRecord::Base
  attr_accessible :mentioned_id

  belongs_to :mentioned, :class_name => "User"
  belongs_to :post

  validates :mentioned, :presence => true
  validates :post, :presence => true

end

And this is the relevant portion of the code for my user class:

    Class User < ActiveRecord::Base

  …

  has_many :posts, :dependent => :destroy  
  has_many :mentions, :dependent => :destroy, :foreign_key => 'mentioned_id'

  …

This also happens in my spec test:

 @user = Factory(:user, :email => Factory.next(:email))
  @other_user = Factory(:user, :email => Factory.next(:email))

  @post1 = Factory(:post, :user => @other_user)
  @post2 = Factory(:post, :user => @other_user)
  @post3 = Factory(:post, :user => @user) 
  @mention1 = @post1.mentions.create :mentioned_id => @user
  @mention2 = @post2.mentions.create :mentioned_id => @user 
  @mention3 = @post3.mentions.create :mentioned_id => @other_user.id

The last line only works correctly if I use the .id method, but the previous two work correctly…

  • 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-25T20:53:47+00:00Added an answer on May 25, 2026 at 8:53 pm

    You’re trying to ask Rails to map an integer (mentioned_id) to an object (@user). You’re doing it correctly on your @post1 = ... lines, where you’re assigning @other_user to :user, not :user_id. You need to do the same on your @mention1 = ... lines:

    @mention1 = @post1.mentions.create :mentioned => @user
    

    However, with your code as it is, this won’t work either, because your attr_accessor line is interfering.

    You can either get rid of the line altogether and the :mentioned => @user part will work, or you can change it like so, to allow assigning directly to :mentioned rather than :mentioned_id (yes, they really are the same thing, but it seems that Rails checks attr_accessible and denies access to updating :mentioned before it realizes that :mentioned is really just :mentioned_id anyway).

    attr_accessible :mentioned_id, :mentioned
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I enjoy developing algorithms using the STL, however, I have this recurring problem where
I'm developing a USB bootloader for an embedded system and one of my requirements
I am developing an accounting system for a customer. This application has some special
I'm developing a general purpose library which uses Win32's HeapAlloc MSDN doesn't mention alignment
We are developing a website quite similar with ebay.com and in order to upgrade/maintain
I'm developing a custom control. One of the requirements is to draw lines. Although
I'm developing a Hotel reservation iPhone application . I tab view that I mention
I'm developing an Android application and I want to recognize hashtags, mentions and links.
Clarification When I mention cross compiling I mean from one language to another (think
I am developing an application for Android devices, and one part of it is

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.