Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

RAILS 3.1 - unknown attribute: id on build


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


Solution

  • In your migration, change line:

    t.integer :option_id
    

    to:

    t.integer :fte_report_option_id