S
S
sonriente2013-11-06 10:57:20
Ruby on Rails
sonriente, 2013-11-06 10:57:20

Error: Unknown attribute in ActiveRecord(ruby). How to fix?

Hello.
There are the following models:

class Contractor < User<br>
  has_many :contractorEs<br>
  has_many :customers<br>
end<br><br>
class Customer < User<br>
  belongs_to :contractor<br>
  has_many :customerEs<br>
end<br>

There is a migration:
class AddContractorIdToCustomers < ActiveRecord::Migration<br>
  def change<br>
    add_column :customers, :contractor_id, :integer<br>
  end<br>
end<br>

I'm trying to create a customer (Customer), for this I go to the console and do the following:
c = Contractor.first<br>
c.customers.build<br>

Instead of the created customer, I get the following error message:
ActiveRecord::UnknownAttributeError: unknown attribute: contractor_id<br>

Migration started, the server restarted. The problem, unfortunately, remains.
Please tell me what could be causing this and how to fix it.
Rays of good to you :)
UPD: Ruby 2/Rails 4
UPD 2: I use STI, everything is inherited from User-a. Maybe the problem is related to this?
UPD 3: I continue to investigate the problem. It turned out that when creating a model through Customer.new, there is no contractor_id in the fields:
<Customer id: nil, first_name: nil, last_name: nil, address: nil, country: nil, zip: nil, city: nil, phone: nil, company_name: nil, signature: nil, type: "Customer", created_at: nil, updated_at: nil, v_token: nil, password_digest: nil, u_token: nil, email: nil, state: "new"><br>

If I inherit Customer from ActiveRecord::Base, then everything works fine. But I need to inherit from User.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
The Whiz, 2013-11-06
@modernstyle

strong_params?

 def contractor_params
    params.require(:contractor).permit(:contractior_id)
 end

D
Dmitry Tsepelev, 2013-11-06
@DmitryTsepelev

what version of rails? if 3, then perhaps you need to add it to Customer

attr_accessible :contractor_id

H
himik, 2013-11-06
@himik

replace

has_many :contractorEs

on the
has_many :contractors

and you will be happy

A
Adil1, 2013-11-26
@Adil1

try making a has_many :through association, http://www.rusrails.ru/active-record-associations, and when migrating it's better to use contactor:references:index

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question