0

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
thiagoh
  • 7,098
  • 8
  • 51
  • 77
  • Is that actual copies of the source code? Because you have "Productor" spelled incorrectly. Could be part of the problem. – Dave Isaacs Nov 27 '12 at 21:44
  • this is not the problem.. Its because I'm brazilian and traduced the question for you to understand – thiagoh Nov 27 '12 at 21:46

2 Answers2

1

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
John Naegle
  • 8,077
  • 3
  • 38
  • 47
  • do I need to create the columns :productor_id and :company_id manually or the rails does it for me? – thiagoh Nov 27 '12 at 21:48
  • another thing.. it is Productor that has a User not the inverse.. so do has_one should be at Productor?? – thiagoh Nov 27 '12 at 21:53
  • 1
    has_one and belongs_to both express 1 to 1 relationships, the difference is where the foreign_key is declared. Your tables had user_id on productor and company so you would declare the models as I have shown. To enforce that a User only has either a productor or a company would require some custom validation. – John Naegle Nov 27 '12 at 21:53
  • but if I do on your way I'll have productor_id and company_id inside Users table.. and I dont want this way.. how can I do? – thiagoh Nov 27 '12 at 21:57
  • I want the user_id inside both Productors and Companys tables – thiagoh Nov 27 '12 at 21:58
  • did you read "Is it as belongs_to or has_one association?" here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Is+it+a+belongs_to+or+has_one+association? – John Naegle Nov 27 '12 at 21:59
  • Yes.. I still have this ` no such column: users.produtor_id: SELECT "users".* FROM "users" WHERE "users"."produtor_id" = 1 LIMIT 1` – thiagoh Nov 27 '12 at 22:06
  • You have your belongs_to and has_one backwards. Change your models to match my answer. – John Naegle Nov 27 '12 at 22:12
-1

Have you made the association in your User model yet?

class User < ActiveRecord::Base
  attr_accessible :email, :password
  belongs_to :productor
  ...
 end
NicSlim
  • 339
  • 3
  • 12