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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:13:27+00:00 2026-06-12T06:13:27+00:00

I have the following :has_many :through relation. Associations class Profile < ActiveRecord::Base has_many :teams

  • 0

I have the following :has_many :through relation.

Associations

class Profile < ActiveRecord::Base  
  has_many :teams
  has_many :projects, :class_name => "Project", :through => :teams
  has_many :leads, :class_name => "Projects"

class Project < ActiveRecord::Base
  has_many :teams
  has_many :developers, :class_name => "Profile", :through => :teams
  belongs_to :lead, :class_name => "Profile", :foreign_key => "developer_lead"

class Team < ActiveRecord::Base
  belongs_to :developer, :class_name => "Profile"
  belongs_to :project

When I try to get a Profiles projects the relationship doesn’t use the right key in the teams table.

Rails C

1.9.3p194 :001 > Profile.first.projects

Profile Load (0.2ms) SELECT “profiles”.* FROM “profiles” LIMIT 1
Project Load (0.2ms) SELECT “projects”.* FROM “projects” INNER JOIN “teams” ON “projects”.”id” =
“teams”.”project_id” WHERE “teams”.”profile_id” = 1

It should be using "teams"."developer_id" = 1

I’ve tried using a :foreign_key => "developer_id" in both the Profile and Project models, but nothing seems to work.

I feel like the changes to the models I’ve been making aren’t taking any effects, after each change I’ve been restarting the rails console though.

Schema

create_table "profiles", :force => true do |t|
  t.datetime "created_at",          :null => false
  t.datetime "updated_at",          :null => false
end

create_table "projects", :force => true do |t|
  t.integer  "developer_lead"
  t.datetime "created_at",     :null => false 
  t.datetime "updated_at",     :null => false
end

create_table "teams", :id => false, :force => true do |t|
  t.integer  "developer_id"
  t.integer  "project_id"
  t.datetime "created_at",   :null => false
  t.datetime "updated_at",   :null => false
end 
  • 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-12T06:13:28+00:00Added an answer on June 12, 2026 at 6:13 am

    :foreign_key => "developer_id" belongs with has_many :teams.

    Also, it makes your code clearer if you stick to rails conventions and end all foreign_key names with “_id”, as in “developer_lead_id”.

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

Sidebar

Related Questions

I have the following models class Project < ActiveRecord::Base has_many :project_members has_many :members, :through
I have the following models: class Campaign < ActiveRecord::Base has_many :campaign_keywords has_many :leads, :through
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following class: class Group < ActiveRecord::Base has_many :users belongs_to :leader, :foreign_key
We have the following model: payment.rb: class Payment < ActiveRecord::Base has_many :currencies default_scope :include
I have the following models: Post.rb class Post < ActiveRecord::Base belongs_to :category has_many :attachments,
I have the following model structure: class Group < ActiveRecord::Base has_many :group_products, :dependent =>
I have the following three models (Rails 2.3.8) class Outbreak < ActiveRecord::Base has_many :incidents,
I have the following: class Menu < ActiveRecord::Base has_many :menu_headers # has_many :menu_headers, :conditions
i have following model setup class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column

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.