2
votes

When I want to use build for a many-to-many through association, I get the following error in my controller:

unknown attribute: fte_report_option_id

In my controller:

def edit_clients_reports
@fte_report_option = FteReportOption.find(params[:id])
@fte_report_option.fte_report_client_options.build
end

In my first model, I have:

class FteReportOption < ActiveRecord::Base

has_many :fte_report_client_options, :dependent => :destroy
has_many :clients, :through => :fte_report_client_options
end

In my second model:

class FteReportClientOption < ActiveRecord::Base
self.primary_key = "client_report_id"

belongs_to :fte_report_option, :foreign_key => :option_id, :class_name => "FteReportOption" belongs_to :client, :foreign_key => :client_id, :class_name => "Client"
end

And my third model:

class Client < ActiveRecord::Base
set_primary_key "client_id"

has_many :fte_report_client_options, :dependent => :destroy
has_many :fte_report_options, :through => :fte_report_client_options

In my migration for the join table, I have:

create_table :fte_report_client_options, :primary_key => "client_report_id", :force => true do |t|
t.integer :option_id
t.integer :client_id
t.timestamps
end

Is anyone know what's happen?

Thanks for your help

1
Perhaps we should begin by reviewing the code that you've posted. In the FteReportClientOption we have self.primary_key = "client_report_id" which doesn't make any sense. Did you mean set_primary_key? Then, in the migration you set the primary key to a column that doesn't exist in the table. Problems like these can affect how Rails tries to "guess" the names of your attributes. In all - I would strongly urge you to use Rails-standard primary keys if you do not have a really strong reason not to.Jesper

1 Answers

0
votes

In your migration, change line:

t.integer :option_id

to:

t.integer :fte_report_option_id