I am creating an app that tracks a users employments and where they are in the companies. I need some help trying to route the app, I has made scaffolds of user, company, and department.
user
company (user has_many :through => employments)
department
user.rb
class User < ActiveRecord::Base
#associations
has_many :employments
has_many :companies, :through => :employments
has_one :department, :through => :employments
end
employment.rb
class Employment < ActiveRecord::Base
belongs_to :user
belongs_to :company
belongs_to :department
has_many :employment_histories
end
employment_history.rb
class EmploymentHistory < ActiveRecord::Base
belongs_to :employment
end
company.rb
class Company < ActiveRecord::Base
has_many :employments
has_many :users, :through => :employments
has_many :departments
end
department.rb
class Department < ActiveRecord::Base
belongs_to :company
end
For routing, the simplest way to do it is to declare your resources as resources, and then you access them through the id of the object.
http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default
Then to find through associations, I suggest you check out this railscast: http://railscasts.com/episodes/3-find-through-association?view=asciicast
If routing and finding through associations doesn't come easily, you will probably want to refactor your hierarchy to make more sense metaphorically. For instance my approach would be:
User has_many :employments and give it default_scope order: 'employments.created_at DESC' in the User model to make sure the most recent employment is first.
Employment has_one :department
Department belongs_to :company
Company has_many :departments
Then you can access all of this data through associations (see link above). You only need to route the resources, and then know how to access them in the right controllers.
Related
I have a memberships resource and it belongs to user and club. I want to access the parent attributes i.e for club and user and I read that accepts_nested_attributes_for is used for parent side of a relationship. What should I write in my membership model?
I have searched about it both in stackoverflow and activeadmin docs but I did not get a thorough explanation about solving my problem...
My membership model is:
membership.rb
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
end
Also what should i write in my membership resource which I have already registered with AA...
You can mention the following :-
1) has_many: memberships #in user model
2) has_many: memberships #in club model
This will help you access parent attributes from child model :-
membership.user, membership.club
Also, you can mention accepts_nested_attributes_for: memberships in user model.
When you write this, you can then build a common form for user and membership and modify both of them simultaneously. To achieve this, you will have to allow membership attributes in users_controller.rb.
The following should work(Similar question):
class Club < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
accepts_nested_attributes_for :membership
end
class User < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :clubs, :through => :memberships
accepts_nested_attributes_for :membership
end
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
accepts_nested_attributes_for :club
end
A list has one owner (a user). A list also has a number of panelists (also users). I have tried defining the relationships between the three models: User, List, and Panelist. But I'm getting nowhere.
user.rb
class User < ActiveRecord::Base
has_many :lists
has_many :panelMemberships, :through => :panelists, :source => :lists
end
list.rb
class List < ActiveRecord::Base
belongs_to :user
has_many :panelMembers, :through => :panelists, :source => :user
end
panelist.rb
class Panelist < ActiveRecord::Base
belongs_to :list
belongs_to :user
end
I've tried all different combinations but nothing seems to work. Thanks in advance for any help you can provide.
The model also has to have a has_many relationship for whatever the through model is, so wherever you have has_many :x, through: :y, you also need to say has_many :y. You also shouldn't have a panelist model separate from your user model if panelists are users (unless you're doing STI, which you're not). From what I understand, you're trying to do something like this:
class User < ActiveRecord::Base
has_many :owned_lists, class_name: "List", foreign_key: :owner_id # this is for the owner/list relationship
has_and_belongs_to_many :lists # for the normal panelist / list relationship
end
class List < ActiveRecord::Base
belongs_to :owner, class_name: "User"
has_and_belongs_to_many :users
end
Then you'll need to make a migration for a users_lists (with user id and list id) table which will be your join table but won't need its own model. But if you really want to keep the through relationship (good for if you do other stuff with the join model), then you'd do:
class User < ActiveRecord::Base
has_many :owned_lists, class_name: "List", foreign_key: :owner_id # this is for the owner/list relationship
has_many :panel_memberships
has_many :lists, through: :panel_memberships
end
class List < ActiveRecord::Base
belongs_to :owner, class_name: "User"
has_many :panel_memberships
has_many :users, through: :panel_memberships
end
class PanelMembership < ActiveRecord::Base
belongs_to :user
belongs_to :list
I am implementing an authentication+authorization system in my Rails 3 Application.
I have a HABTM relationship with between Users and Roles.
The roles I will have are : manager,dealer,operator,admin
Now a manager can have many dealers under him. How do I model this relationship?
It is a simple question but I could not find an answer. Also a similar question here: Role-dependent associations but it does not clear things properly.
EDIT:
I am thinking my requirement is further simple. For me a user can only be either an Admin, Operator, Dealer or Manager.
For this I can simply add a role column to User table. How will the relationship between Managers and Dealers be enforced now?
I think you're looking for something more like
class Organization < ActiveRecord::Base
has_many :users
has_many :managers
has_many :dealer
has_many :admins
has_many :operators
has_many :dealer_users, :through => :managers, :class_name=>"User"
end
class Admin < ActiveRecord::Base
has_many :organizations
belongs_to :user
end
class Dealer
has_many :organizations
belongs_to :user
end
class Operator < ActiveRecord::Base
has_many :organizations
belongs_to :user
end
class Manager < ActiveRecord::Base
has_many :dealers
belongs_to :organization
end
class User < ActiveRecord::Base
has_many :organizations
has_many :admins
has_many :operators
has_many :managers
has_many :dealers
end
In my rails application Company acts as the user model. A company can have many customers and many employees (carseller).
There is a many to many relation between a carseller and a customer.
There is the following problem: A lot of times i'd have to retrieve all the appointments made with the whole company. Since there is no company_id saved in the appointment model this can be quite painful.
Should i just include a foreign key to company in the appointments and have some form of redundancy or is there another easy and efficient way?
class Company < ActiveRecord::Base
has_many :customers
has_many :carseller
end
class Customer < ActiveRecord::Base
belongs_to :company
has_many :appointments
has_many :carsellers, :through => :appointments
end
class Carseller < ActiveRecord::Base
belongs_to :company
has_many :appointments
has_many :customers, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
end
I think you can use :
class Appointment < ActiveRecord::Base
belongs_to :customer
belongs_to :carseller
has_one :company, :through => :carseller
end
Then you just have to do appointment.company to get it.
I have 5 classes that are part of a point of sales program:
Customer, Technician, Order, Ticket, Service
Each Order has one customer and many tickets. Each Ticket has on technician and many services.
How should I make the associations so that I can do the following lookup:
customer.order.find_by_date(10/20/2010).service.technician.name
This is what I am planning:
class Customer < ActiveRecord::Base
has_many :orders
has_many :tickets, :through => :orders
has_many :technicians, :through => :tickets
end
class Technician < ActiveRecord::Base
belongs_to :ticket
belongs_to :service
end
class Service < ActiveRecord::Base
has_many :technicians
belongs_to :ticket
end
class Ticket < ActiveRecord::Base
has_many :services
has_many :technicians, :through => :services
end
class Order < ActiveRecord::Base
has_many :tickets
has_many :services, :through => tickets
has_many :technicians, :through => services
belongs_to :customer
end
The relationship between customer and order is one-to-many. And the relationship between order and service is also one-to-many. Therefore you cannot do:
customer.order.find_by_date('10/20/2010').service.technician.name
It should be:
customer.orders.find_by_date('10/20/2010').services.each do |service|
service.technician.name
end
It's a similar problem as in your other question.