How to set up multiple aliased joins between two models? - ruby-on-rails-3

In a Rails 3.2 app I need to set up two associations between the same two models.
For example
class User
has_many :events
has_many :attendances
has_many :attended_events, through: :attendances
end
class Event
belongs_to :event_organizer, class_name: "User"
has_many :attendances
has_many :attendees, through: :attendances, class_name: "User"
end
class Attendance
belongs_to :attended_event, class_name: "Event"
belongs_to :attendee, class_name: "User"
end
This is the first time I've experimented with aliasing class names, and I'm having trouble getting it to work. I'm not sure if the issue lies with how I've defined the associations, or elsewhere.
Do my associations look OK? Have I overlooked something needed to get this to work?
The problem I'm having is that no user ids are being set in the Attendance model. This may be a stupid question, but given my associations above, should the field name be :user_id or :event_organizer_id?
Really appreciate any suggestions on this. Thanks!

foreign_key to be specified in User and Event
class User
has_many :events
has_many :attendances, foreign_key: :attendee_id
has_many :attended_events, through: :attendances
end
class Event
belongs_to :event_organizer, class_name: "User"
# here it should be event_organizer_id
has_many :attendances, foreign_key: :attended_event_id
has_many :attendees, through: :attendances
end
class Attendance
belongs_to :attended_event, class_name: "Event"
# this should have attended_event_id
belongs_to :attendee, class_name: "User"
# this should have attendee_id because of 1st param to belongs_to here is "attendee"
# and same should be added as foreign_key in User model
# same follows for Event too
end

Related

Best approach for assigning user roles for different groups in Rails 5?

Here are the basic models without roles:
group model
class Group < ApplicationRecord
has_many :memberships
has_many :users, through :memberships
end
user model
class User < ApplicationRecord
has_many :memberships
has_many :groups, through :memberships
end
memberships model
class Membership < ApplicationRecord
belongs_to :group
belongs_to :user
end
Each group should have 1 (and only one) owner, multiple admins (assigned by the owner), and the rest general members. Also, the ownership role needs to be transferable. My options, as I see them:
Create a single attribute role on the membership table and assign it a string value of "owner", "manager", or "general". [bad]
Create multiple booleans for "owner", "manager", and "general" on the membership table. [bad]
Create a Role model/table with 1 column ("name") and 3 rows ("owner", "manager", "general") then update my models like so:
Role model
class Role < ApplicationRecord
has_many :memberships
has_many :users, through :memberships
has_many :groups, through :memberships
end
group model
class Group < ApplicationRecord
has_many :memberships
has_many :users, through :memberships
has_many :roles, through :memberships
end
user model
class User < ApplicationRecord
has_many :memberships
has_many :groups, through :memberships
has_many :roles, through :memberships
end
memberships model
class Membership < ApplicationRecord
belongs_to :group
belongs_to :user
belongs_to :role
end
Create separate arrays for the various roles directly on the group model. This seems really stupid, as updating a role would require the pushing and splicing of multiple arrays, and I would have to concatenate multiple arrays to present a simple list of memberships.
group model
class Group < ApplicationRecord
has_many :general_memberships, class_name: 'Membership'
has_many :admin_memberships, class_name: 'Membership'
has_one :owner_membership, class_name: 'Membership'
has_many :users, through :memberships
end
user model
class User < ApplicationRecord
has_many :general_memberships, class_name: 'Membership'
has_many :admin_memberships, class_name: 'Membership'
has_many :owner_memberships, class_name: 'Membership'
has_many :groups, through :memberships
end
memberships model
class Membership < ApplicationRecord
belongs_to :group
belongs_to :user
end
I know there are gems out there like CanCanCan (Rails 5 compatible?) and Groupify, but I want to understand all my options first. I think option #3 is probably my best best, at least without without utilizing a gem. Wondering what the community considers best practice for my scenario.
UPDATE
I ended up going with the following solution and have been very satisfied with it in a production app. Since a group can only have one owner, it made since to use a belongs_to relationship for that role.
For the Membership model, I took advantage of active record enum. Since the enum array only holds two values, it actually would have been more efficient to simply add a "manager" boolean to the membership model. True means manager, false means general. But I went with the enum approach, because I anticipate needing additional roles in the near future, and using enum they will be VERY easy to add.
group model
class Group < ApplicationRecord
belongs_to :owner, class_name: 'User'
has_many :memberships
has_many :users, through :memberships
end
user model
class User < ApplicationRecord
has_many :owned_groups, class_name: 'Group', :foreign_key => 'owner_id'
has_many :memberships
has_many :groups, through :memberships
end
membership model
class Membership < ApplicationRecord
belongs_to :group
belongs_to :user
enum role: [:general, :manager]
end

Modelling a team manager through a membership table

I have the following Rails models connecting teams and users:
class User < ActiveRecord::Base
has_many :memberships
has_many :teams, through: :memberships
end
class Team < ActiveRecord::Base
has_many :memberships
has_many :users
end
class Membership < ActiveRecord::Base
belongs_to :team
belongs_to :user
end
I want to implement a team manager via the existing memberships table.
Would it be a better idea to have a foreign key in the teams table like this:
class Team < ActiveRecord::Base
belongs_to :manager_membership, class_name: 'Membership'
has_one :manager, through: :manager_membership, source: :user
# ...
end
Or would it be better to have an extra column (with unique index) on memberships like this:
class Team < ActiveRecord::Base
has_one :manager_membership, -> where { manager: true }, class_name: 'Membership'
has_one :manager, through: :manager_membership, source: :user
# ...
end
I wonder if having foreign keys going both ways from team to a membership and back is weird? Or maybe it doesn't make much difference either way?
Based on the comments, you should be able to use a single table inheritance for both your users and managers. That would mean you get to use the same membership relationship on both models.
class User < ActiveRecord::Base
has_many :memberships
has_many :teams, through: :memberships
end
class Manager < User
# your code
end
You can now use Manager wherever needed, and add any validations directly on that model. Remember to add a type column to your User table if you decide to go this route.
EDIT
Looking at this again, you will need to have a separate table to keep the relationship between your Manager's team and the actual team they manage clear. Yourteam` should be something like:
class Team < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
has_many :teams_managers
has_many :managers, through: :teams_managers
end
class Manager < User
has_many :teams_managers
# note here we're aliasing for clarity and specifying a source
has_many :managed_teams, through: :teams_managers, source: :team
end
# this is your association model for managers <-> teams
class TeamsManager < ActiveRecord::Base
belongs_to :manager
belongs_to :team
end
# so now you could call something like this
manager = Manager.find 1
manager.managed_teams =>[...]
manager.teams =>[...]
You'd have to make the corresponding migrations to instantiate your TeamsManagers table as well.

Rails: NameError: uninitialized constant on join table

I can't seem to work around the name convention or if I'm joining them incorrectly.
This is the error I'm getting from the user model:
> user.companies
NameError: uninitialized constant User::CompaniesUser
And from the company model:
> company.users
NameError: uninitialized constant Company::CompaniesUser
user.rb
has_many :companies_users
has_many :companies, :through => :companies_users
company.rb
has_many :companies_users
has_many :users, :through => :companies_users
company_user.rb
class CompanyUser < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
I've been looking up through examples but I honestly don't understand why it keeps exploding. If there's any additional information needed, I'll provide it, I appreciate any help given.
Your association companies_users will be mapped to class named CompaniesUser by Rails because "companies_users".classify will give you CompaniesUser. But the class you want to associate is CompanyUser, so the solution in this case would be to modify your associations to include class_name option as follows:
# user.rb
has_many :companies_users, class_name: CompanyUser
has_many :companies, :through => :companies_users
# company.rb
has_many :companies_users, class_name: CompanyUser
has_many :users, :through => :companies_users
Update: This is of course if you want to stick with the association name companies_users, otherwise #Babur has a solution for you.
It should be:
has_many :company_users
has_many :companies, :through => :company_users
Only last word should be pluralized
Because of your has_many :companies_users in your Company model, Rails tried to load a model class for that table, that be convention would be called CompaniesUser. To make your code work, you could either change your has_many declaration to
has_many :company_users
or even get rid of the CompanyUser model completely and use has_and_belongs_to_many instead.
class User
has_and_belongs_to_many :companies
end
class Company
has_and_belongs_to_many :users
end

How do I set up a has_one through association through a has_many through association?

I have a User model, a Membership model, and a Club model. I have set up the following associations:
Club.rb
has_many :memberships
has_many :members, through: :memberships
Membership.rb
belongs_to :user
belongs_to :club
What I want now is to have a has_one :organizer on Club that retrieves a single User record based on the member with the organizer attribute set to true in the join model.
How do I set up this has_one association? I've tried going through the memberships association, but an exception is raised that the memberships is a collection and needs to be singular.
I think I just figured it out...but in case someone else needs to know..
has_one :membership, conditions: ['memberships.organizer = ?', true]
has_one :organizer, through: :membership, source: :user

rails - what is the best/most efficient way to associate these models

In my application I have the following situation:
User belongs_to Group
Group has_many Users
Project belongs_to User
User has_many projects
The following is also true:
- Each Group will have one BaseCase
- Each Project will have multiple Scenarios and one BaseCase(depending on the Group the Project User belongs to)
- Scenario and BaseCase is the same type of object (let's call this Data)
- The default values for each Scenario are the BaseCase values for the Group, but the User can change these default values to create the specific Scenario
I am not sure how to capture all these relationships through associations efficiently, does anyone have any ideas? thanks
If I understand correctly, then something like that
class User
belongs_to :group
has_many :projects
end
class Group
has_many :users
has_many :projects, :through => :users
has_one :base_case
end
class Project
has_many :scenarios
has_one :base_case
belongs_to :user
has_one :group, :through => user
has_one :base_case, :through => :group
end
class Scenario
belongs_to :project
has_one :base_case, :through => :project
before_create do
self.attributes = self.base_case.attributes.except(:group_id, ...)
end
end
class BaseCase
belongs_to :group
end