N:through:1 Relation or n:m with another table

Hello,

currently I have 3 tables: users, projects and customers but customers

soon should become part of users too so I decided to create a relation

through projects but has_many :through doesn’t work in this case,

because AR generated me a wrong WHERE clause.

create_projects.rb

create_table “projects”, :force => true do |t|
t.column “title”, :string, :null => false
t.column “description”, :text
t.column “fee”, :integer, :default => 2000, :null => false
t.column “tax”, :integer, :default => 19, :null => false
t.column “client_id”, :integer, :null => false
t.column “agent_id”, :integer, :null => false
end

class User < ActiveRecord::Base
has_many :clients, :through => :projects
#…
end

class Project < ActiveRecord::Base
belongs_to :client, :class_name => ‘Customer’, :foreign_key
=> :client_id
belongs_to :agent, :class_name => ‘User’, :foreign_key => :agent_id
#…
end

The definition of an class_name or the relation from User class itself

seems to baffle AR to build a wrong WHERE clause, although I defined a

foreign_key in projects relation…

So I tried to use a n:m relation, which seems to fit better, but I

always

get duplicates which I could not get rid off using :uniq => true.

user.rb

class User < ActiveRecord::Base
has_and_belongs_to_many :clients,
:class_name => ‘Customer’, # Becomes a User in future
:association_foreign_key => :client_id,
:foreign_key => :agent_id,
:join_table => :projects,
:uniq => true # !! Doesn’t seem to work :frowning:
#…
end

clients = user.clients.uniq # !! Doesn’t work too :frowning:

Sincerely
Florian