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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:43:59+00:00 2026-05-23T16:43:59+00:00

I have my models setup like so: class User < ActiveRecord::Base has_many :posts, :foreign_key

  • 0

I have my models setup like so:

class User < ActiveRecord::Base
  has_many :posts, :foreign_key => 'author_id'
end

class Post < ActiveRecord::Base
  belongs_to :author, :class_name => 'User'
end

Assuming:

p = Post.first # just any post instance
a = User.first # any user instance

Now this piece of code is acting very weird

p.author = a

After setting the author, the attribute author_id of the post should be set to the id of the user. But this isn’t happening.

I tried using models with belongs_to that does not have the class_name parameter and everything works as expected.

Now, one more thing that makes it weirder is that when I change the association to belongs_to :author, :class_name => 'User', :foreign_key => 'author_id', it surprisingly works.

Is this a bug in Rails 3.0.9? Shouldn’t the foreign key parameter be unnecessary because as the docs say, its default value is the name of the association appended with _id.

Also note that even without :foreign_key => 'author_id', everything else regarding the association works as expected. (Like fetching the associated model) The only thing not working is the setter method not setting the foreign key.

I know I could just do p.author_id = a.id or just add :foreign_key params to all my associations with class_name, but I prefer the more elegant syntax of p.author = a

  • 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-23T16:44:00+00:00Added an answer on May 23, 2026 at 4:44 pm

    After reading through lots of Rails code and tracing here’s what I found:

    This bug exists because of the gem composite_primary_keys which overrode the default rails reflection.rb.

    I will have to check how they implemented the primary_key_name and derive_primary_key_name methods.

    It was quite a bit of time wasted on this silly bug, but at least I got to learn a lot about ActiveRecord’s internals.

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

Sidebar

Related Questions

I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have a two models set up like this: class User < ActiveRecord::Base #
What I have now: class User < ActiveRecord::Base has_many :people end ... and... class
I have the following models, with their relevant associations: class User < ActiveRecord::Base has_many
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
Here are my models: class BillingProfile < ActiveRecord::Base belongs_to :student attr_accessible :cost end class
First here's my current setup: models/user.rb class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable,
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
I have a class Book : from django.db import models from users.models import User
It seems simple, in my model I have: class CustomerAccount < ActiveRecord::Base acts_as_url :name

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.