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
Related
I want a user to be able to create and save or choose an existing client to add to an invoice. There can only be one client per invoice.
I currently have 3 models users invoices and items
I am using a simple has_many relationship currently but I am getting confused now that I want to add a new table clients. I was hoping I could get some advice on what association to use.
My current associations
class User < ActiveRecord::Base
has_many :invoices
class Invoice < ActiveRecord::Base
belongs_to :user
has_many :items, :dependent => :destroy
class Item < ActiveRecord::Base
belongs_to :invoice
So I was thinking to do something simple like adding has_many :clients
to users, add has_one :client to invoices and add the table clients
class User < ActiveRecord::Base
has_many :invoices
has_many :clients
class Client < ActiveRecord::Base
belongs_to : user
class Invoice < ActiveRecord::Base
belongs_to :user
has_many :items, :dependent => :destroy
has_one :client
Would this work? Is there a better way?
It is very rare that you will use has_one. In your case the following models make more sense:
class User < ActiveRecord::Base
has_many :invoices
end
class Invoice < ActiveRecord::Base
belongs_to :user
belongs_to :client
has_many :items, :dependent => :destroy
end
class Item < ActiveRecord::Base
belongs_to :invoice
end
class Client < ActiveRecord::Base
has_many :invoices
has_many :items, through: :invoices
end
In my Rails app I have Users and Forms.
class User < ActiveRecord::Base
has_many :admin_roles
#desired association below
#has_many :forms, through: :admin_roles
end
class Form < ActiveRecord::Base
has_one :department
end
The Users need to administrate the Forms through any level of an Organization.
class AdminRole < ActiveRecord::Base
belongs_to :organization
belongs_to :user
end
If assigned to a non-department organization the forms they have control over should come through the child departments.
The Forms are assigned to a departmental level only.
My model for the Organization is an STI model of 3 levels: market>subdomain>department
class Organization < ActiveRecord::Base
self.inheritance_column = :org_level
has_many :admin_roles
end
class Department < Organization
belongs_to :sub_domain, primary_key: :id, foreign_key: :parent_id
has_many :forms
end
class SubDomain < Organization
belongs_to :market, primary_key: :id, foreign_key: :parent_id
has_many :departments
end
class Market < Organization
has_many :sub_domains
end
The desired capability is to do user.forms and get all the associated forms back.
For example: Given there was a hierarchy of FooMarket>BarDomain>LoremDepartment
and a Form associated to LoremDepartment.
If a User is then tied to any of those 3 Organizations through the AdminRole it would allow for the return of the LoremDepartment Form.
Do you have to necessarily do it with associations ? u can always define an instance method in user model and back track it to forms.
But before that, just a reminder, you have to mention the foreign key in both the models for the association to work both ways.
class User < ActiveRecord::Base
attr_accessible :name
has_many :admin_roles
has_many :organizations, :through => :admin_roles
def forms
organizations.map(&:forms).flatten.uniq
end
end
class Department < Organization
belongs_to :sub_domain, primary_key: :id, foreign_key: :parent_id
has_many :forms, :foreign_key => :organization_id
end
class SubDomain < Organization
belongs_to :market, primary_key: :id, foreign_key: :parent_id
has_many :departments, foreign_key: :parent_id
def forms
departments.map(&:forms).flatten
end
end
class Market < Organization
has_many :sub_domains, foreign_key: :parent_id
def forms
sub_domains.map(&:forms).flatten
end
end
I tested this and it does work. But kinda round about.
I have these models and associations. I want to reach roleable trough privilege model doesnt matter what roleable_type is(Dj or Photographer)? Use join model because privilege model will have other attributes. It is possible something like this:
class User
has_one :privilege, dependent: :destroy
has_one :roleable, through: :privilege
end
class Dj < ActiveRecord::Base
has_one :privilege
has_one :user, through: :privilege, as: :roleable
end
class Photographer < ActiveRecord::Base
has_one :privilege
has_one :user, through: :privilege, as: :roleable
end
class Privilege < ActiveRecord::Base
belongs_to :user
belongs_to :roleable, polymorphic: true
end
If i add source_type: 'Dj' to has_many :through return only with roleable_type 'Dj'. I want to do this bellow:
u = User.first
u.roleable #return privilage roleable( doesnt matter Dj or Photograher)
I'd make those belongs_to, not that that changes anything.
class User < ActiveRecord::Base
has_one :privilege, dependent: :destroy
has_one :roleable, through: :privilege
end
class Dj < ActiveRecord::Base
has_one :privilege
belongs_to :user, through: :privilege, as: :roleable
end
class Photographer < ActiveRecord::Base
has_one :privilege
belongs_to :user, through: :privilege, as: :roleable
end
class Privilege < ActiveRecord::Base
belongs_to :user
belongs_to :roleable, polymorphic: true
end
Can you post, what u.roleable returns?
I have the following models
Class User
has_many :memberships
Class Membership
belongs_to :user
The membership table has two columns, user_id and organization_id.
However the organization_id is also a user id.
How can I specificy an association so that:
#user.organizations returns a list of organizations(other users) of which this user is a member.
I think the correct way of doing this would be to create three models as follows:
#user.rb
class User < ActiveRecord::Base
has_many :memberships
has_one :organization
end
#organization.rb
class Organization < ActiveRecord::Base
attr_accessible :user_id
belongs_to :user
has_many :memberships
end
#membership.rb
class Membership < ActiveRecord::Base
attr_accessible :user_id, :organization_id
belongs_to :user
belongs_to :organization
end
Figured it out:
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :organization, class_name: "User"
end
class User < ActiveRecord::Base
has_many :memberships
has_many :organizations, through: :memberships
end
I'm new to rails and working on an app that has the following situation:
Users have skills (e.g rafting, dancing)
Users participate in contests
Contest measures multiple skills
At the end of each contest, each user gets a score (e.g dancing: 5, rafting: 4)
Whats the best way to model this ?
Thanks,
This got nasty :s At the end I was actually not sure if this is the right way
class Skill < ActiveRecord::Base
has_many :skill_scores
has_many :user_skills
end
class UserSkill < ActiveRecord::Base
belongs_to :user
belongs_to :skill
end
class SkillScore < ActiveRecord::Base
belongs_to :user
belongs_to :contest
belongs_to :skill
end
class User < ActiveRecord::Base
has_many :skills
has_many :contests, :through => :contest_participations
has_many :skill_scores
end
class Contest < ActiveRecord::Base
has_many :users, :through => :contest_participations
has_many :skill_scores
end
class ContestParticipation < ActiveRecord::Base
belongs_to :user
belongs_to :contest
end