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
Related
I have three models : User, Agency and Client.
Currently,
class User < ActiveRecord::Base
has_one :agency
has_one :client
end
class Client < ActiveRecord::Base
belongs_to :users
end
class Agency < ActiveRecord::Base
belongs_to :users
end
I want to change associations and create a polymorphic association such as this:
User belongs_to :role , :polymorphic => true
and
Client has_one :user, as: :role
Agency has_one :user, as: :role
I am a novice rails developer. How can I achieve this? HOw to write a migration?
You need to add two fields, role_id and role_type in user model. You can create new migration as follows
rails g migration addNewFieldsToUsers role_id:integer role_type:string
After running rake db:migrate you need to modify the associations as follows
class User < ActiveRecord::Base
belongs_to :role, polymorphic: true
end
class Client < ActiveRecord::Base
has_one :user, as: :role, class_name: 'User'
end
class Agency < ActiveRecord::Base
has_one :user, as: :role, class_name: 'User'
end
Now restart the rails server.
A migration is not needed. There are no associations between models in the database (which is what a migration would change).
What you need to do is change the models app/models/user.rb and app/models/location.rb. simply remove the belongs_to: from user and add it in loction: belongs_to: user.
I'm trying to access the title of a user's role in a view. I want to be able to do this user.user_details.role.title.
user.user_details.role gives me the error undefined methodrole' for #`
What's wrong with my associations that this is not working?
class User < ActiveRecord::Base
has_one :user_details, :dependent => :destroy
has_one :role, :through => :user_details
end
class UserDetails < ActiveRecord::Base
belongs_to :user
has_one :role
end
class Role < ActiveRecord::Base
belongs_to :user_details
end
Your naming for UserDetails model is wrong. Change it to UserDetail & also update in the association.
Then you will be able to access the role by user.user_detail.role. Because for has_one association you have to use singular names.
class User < ActiveRecord::Base
has_one :user_detail, :dependent => :destroy
has_one :role, :through => :user_detail
end
class UserDetail < ActiveRecord::Base
belongs_to :user
has_one :role
end
class Role < ActiveRecord::Base
belongs_to :user_detail
end
I have a data model that looks like this:
A customer has subscription_id and setup_id as parameters. In some cases, customer will only have one of the parameters. In other cases, it will have both.
Currently, if I make a new customer through either the subscriptions flow or the setups flow, either Subscription.last or Setup.last will reflect the most recent customer that was created (with customer_id equalling the last customer created)
However, I am having the problem of Customer.setup_id or Custumer.subscription_id being nil in all cases.
Here's my code from both subscription.rb and setup.rb:
class Subscription < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
class Setup < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
And in customer.rb:
class Customer < ActiveRecord::Base
attr_accessible :email, :name, :stripe_token, :subscription_id, :setup_id, :phone, :plan
has_one :subscription
has_one :setup
end
I'm not sure what I'm doing incorrectly here but I'd love it if the three data models could talk to each other correctly.
Edit: Is it bad that both setup and subscription belong to :user rather than :customer?
Edit 2: Updated the code of setup.rb and subscriptions.rb to correctly reflect the data model currently. And customer.rb is still not recognizing the correct setup_id or subscription_id
class Subscription < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
class Setup < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
class Customer < ActiveRecord::Base
attr_accessible :email, :name, :stripe_token, :phone, :plan
has_one :subscription
has_one :setup
end
customer = Customer.first
customer.subscription # Instance of Subscription that belongs to customer
customer.setup # Instance of Setup that belongs to customer
Im setting up a reminder service that sends deals via email in relation to a persons interests AND city.. Basically, the user inputs important dates (friends bday, anniversary ect) and the interests of that special person.
I want to send them deals based on 1)the users city and 2)the interests of the related person
How should i setup my associations for the Deal model?
What i have so far..
class User < ActiveRecord::Base
belongs_to :city
has_many :person_interests, :as => :person
has_many :interests, :through => :person_interests
end
class City < ActiveRecord::Base
attr_accessible :name
belongs_to :province
has_many :users
end
class PersonInterest < ActiveRecord::Base
belongs_to :interest
belongs_to :person, :polymorphic => true
end
class Interest < ActiveRecord::Base
has_many :person_interests
end
Thanks!
If a deal could apply to more than one interest, you'd start with something like:
class Deal < ActiveRecord::Base
belongs_to :interests
belongs_to :city
end
class City < ActiveRecord::Base
attr_accessible :name
belongs_to :province
has_many :users
has_many :deals
end
class Interest < ActiveRecord::Base
has_many :person_interests
has_many :deals
end
And then you could do something like
#relevant_deals = #city.deals.where(:interest_id => 'abc')
or
#relevant_deals = #interest.deals.where(:city_id => 'def')
For a CRM app, I want to be able to associate a Person model directly to an Account model or to a Company model which in turn is associated to an Account model. Also, I want to associate an Address model to either a Company or a Person. This is what I have in mind:
class Account
has_many :Persons
has_many :Companies
end
class Person
belongs_to :Account
belongs_to :Company
has_one :Address
end
class Company
belongs_to :Account
has_many :Persons
has_one :Address
end
class Address
belongs_to :Person
belongs_to :Company
end
So an Account would be either a "person account" or a "business account" depending on the association. They would be mutually exclusive. I plan to have the foreign keys account_id and company_id in the Person table. By the same token I would have the foreign keys person_id and company_id in the Address table. One foreign key would be null in each case.
Is this okay in Rails? If not, any recommendations would be greatly appreciated.
Take a look at polymorphic associations. I think that's what you are looking for:
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
class Account
belongs_to :User, :polymorphic => true
end
class Person
belongs_to :Account, :as => :User
belongs_to :Company
has_one :Address, :as => :User
end
class Company
belongs_to :Account, :as => :User
belongs_to :Persons
has_one :Address, :as => :User
end
class Address
belongs_to :User, :polymorphic => true
end
...
Greetings Sven