Вопрос

I have models Productor, Company and User

I want that a productor AND a company have both an user. An user that belongs to a Company belongs only to this company. An user that belongs to a Productor belongs only to this productor.

so the tables I want to be this way

productor           company                user
---------           --------               ------
id                  id                     id
name                name                   email
user_id             user_id                password

I tried to do this with has_one association but I got this error

no such column: users.produtor_id: SELECT  "users".* FROM "users"  WHERE "users"."produtor_id" = 1 LIMIT 1

and my model follows

class Produtor < ActiveRecord::Base

  attr_accessible :borndate, :cpf_cnpj, :is_company, :name, :rg
  has_one :user
  ...


class User < ActiveRecord::Base
  attr_accessible :email, :password
  ...
end
Это было полезно?

Решение

I think you have has_one and belongs_to mixed up.

Take a look at Is it a belongs to or has_one association here

with the columns you have:

class Productor < ActiveRecord::Base
  belongs_to :user, :inverse_of => :productor
end

class Company < ActiveRecord::Base
  belongs_to :user, :inverse_of => :company
end

class User < ActiveRecord::Base
  has_one :productor, :inverse_of => :user
  has_one :company, :inverse_of => :user
end

Другие советы

Have you made the association in your User model yet?

class User < ActiveRecord::Base
  attr_accessible :email, :password
  belongs_to :productor
  ...
 end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top