how do I get foreign_key to work in this simple has_many, belongs_to relationship?

Posted by rpflo on Stack Overflow See other posts from Stack Overflow or by rpflo
Published on 2010-03-10T21:48:17Z Indexed on 2010/03/11 5:53 UTC
Read the original article Hit count: 309

I'm pulling data from Harvest. Here are my two models and schema:

# schema
create_table "clients", :force => true do |t|
  t.string   "name"
  t.integer  "harvest_id"      
end

create_table "projects", :force => true do |t|
  t.string   "name"
  t.integer  "client_id"
  t.integer  "harvest_id"
end

# Client.rb
has_many :projects, :foreign_key => 'client_id' # not needed, I know

# Project.rb
belongs_to :client, :foreign_key => 'harvest_id'

I'm trying to get the Projects to find their client by matching Project.client_id to a Client.harvest_id. Here is what I'm getting instead.

> Project.first.client_id
=> 187259

Project.first.client
=> nil

Client.find(187259).projects
=> []

Is this possible? Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about foreign-key-relationship