Rails references with custom column name - ruby-on-rails

Here are two models:
class Team < ApplicationRecord
has_many :matches
end
# Model
class Match < ApplicationRecord
belongs_to :home_team, class_name: 'Team', foreign_key: 'home_team_id'
belongs_to :away_team, class_name: 'Team', foreign_key: 'away_team_id'
end
# Migration
class CreateMatches < ActiveRecord::Migration[5.2]
def change
create_table :matches do |t|
t.references :home_team, references: :team, foreign_key: { to_table: :teams}
t.references :away_team, references: :team, foreign_key: { to_table: :teams}
t.timestamps
end
end
end
Now I want to access to matches of a certain team, so I do it like this:
Team.first.matches
But obviously, it doesn't work because there is no 'team_id' column in matches table. So, how to get access to all matches of a certain team?

You can always just have your own method on the model.
class Team < ApplicationRecord
def matches
Match.where('home_team_id = :team_id OR away_team_id = :team_id', team_id: self.id)
end
end
You will of course miss out on some goodies has_many provides, but you can use this easily in your views/controllers, with extra where or order as needed.
= Team.find(1).matches.where(...).order(...)

Related

Rails Many to Many with Extra Column

So I have these tables:
create_table :users do |t|
t.string :username
t.string :email
t.string :password_digest
t.timestamps
end
create_table :rooms do |t|
t.string :name
t.string :password
t.integer :size
t.integer :current_size
t.timestamps
end
create_table :rooms_users do |t|
t.belongs_to :user, index: true
t.belongs_to :room, index: true
t.boolean :is_admin
t.timestamps
end
I made it so, when I call Room.find(1).users I get a list of all the users in the room. However, I also want to be able to call something like Room.find(1).admins and get a list of users that are admins (where is_admin in rooms_users is true). How would I do that?
Thank you for your time!
You want to use has_many through: instead of has_and_belongs_to_many. Both define many to many associations but has_many through: uses a model for the join rows.
The lack of a model makes has_and_belongs_to_many very limited. You cannot query the join table directly or add additional columns since the rows are created indirectly.
class User < ApplicationRecord
has_many :user_rooms
has_many :rooms, through: :user_rooms
end
class Room < ApplicationRecord
has_many :user_rooms
has_many :users, through: :user_rooms
end
class UserRoom < ApplicationRecord
belongs_to :user
belongs_to :room
end
You can use your existing schema but you need to rename the table users_rooms to user_rooms with a migration - otherwise rails will deride the class name as Rooms::User.
class RenameUsersRooms < ActiveRecord::Migration[5.0]
def change
rename_table(:users_rooms, :user_rooms)
end
end
However, I also want to be able to call something like
Room.find(1).admins and get a list of users that are admins (where
is_admin in rooms_users is true). How would I do that?
You want to use a left inner join:
User.joins(:user_rooms)
.where(user_rooms: { room_id: 1, is_admin: true })
To roll that into the class you can setup an association with a scope applied:
class Room < ApplicationRecord
has_many :user_rooms
has_many :users, through: :user_rooms
has_many :user_room_admins, class_name: 'UserRoom', ->{ where(is_admin: true) }
has_many :user_room_admins, through: :user_rooms,
class_name: 'User',
source: :user
end
You can define a proc in the has_many relation to set SQL clauses, like ORDER or WHERE:
# room.rb
has_many :rooms_users, class_name: 'RoomsUser'
has_many :users, through: :rooms_users
has_many :admins,
proc { where(rooms_users: { is_admin: true }) },
through: :rooms_users,
class_name: 'User',
source: :users
# user.rb
has_many :administrated_rooms,
proc { where(rooms_users: { is_admin: true }) },
through: :rooms_users,
class_name: 'Room',
source: :rooms
You can simplify this with a simple scope defined in the RoomsUser model, something like:
# rooms_user.rb
scope :as_admins, -> { where(is_admin: true) }
And use it in the proc:
# user.rb
has_many :administrated_rooms,
proc { as_admins },
through: :rooms_users,
class_name: 'Room',
source: :rooms
source option explained:
With source: :users, we're telling Rails to use an association called :users on the RoomsUser model (as that's the model used for :rooms_users).
(from Understanding :source option of has_one/has_many through of Rails)

ActiveModel::MissingAttributeError when using namespaces for models

So, I have a namespace for a part of the project.
class Namespace::Product < ActiveRecord::Base
belongs_to: :namespace_category, class_name: 'Namespace::Category'
...
end
class Namespace::Category < ActiveRecord::Base
has_many :products, :class_name => 'Namespace::Product'
...
end
The migration for Product looks like
create_table :namespace_products do |t|
t.belongs_to :namespace_category, index: true
...
end
So, when I do this
p = Namespace::Product.create(some_params)
c = Namespace::Category.find(id)
c.products << p
it throws me an error, saying ActiveModel::MissingAttributeError: can't write unknown attribute 'category_id', however I have an attribute
t.integer "namespace_category_id"
in my schema.rb, which was created by the migration.
For anyone on Rails 5+ looking back at this, you can also specify the namespace on the migration level if you don't want them on the associations:
def change
create_table :namespace_products do |t|
t.references :category, foreign_key: { to_table: :namespace_categories }
end
end
# or
def change
add_reference :namespace_products, :category, foreign_key: { to_table: :namespace_categories }
end
class Namespace::Product < ActiveRecord::Base
belongs_to :category
end
class Namespace::Category < ActiveRecord::Base
has_many :products
end
https://api.rubyonrails.org/v5.0.0/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_reference
I would just omit the namespaces from the associations:
class Namespace::Product < ActiveRecord::Base
belongs_to :category, class_name: 'Namespace::Category'
...
end
class Namespace::Category < ActiveRecord::Base
has_many :products, class_name: 'Namespace::Product'
...
end
class FixColumnName < ActiveRecord::Migration
def change
rename_column :namespace_products, :namespace_category_id, :category_id
end
end
Okay, I just had to specify foreign_key while defining has_many :products association in Namespace::Category, thanks to Max

Has one and has many of itself

I have a user instance that has many invitees but only one inviter.
I am trying to access the inviter instance associated with that user and also his invitees.
i.e:
user.inviter #=> return another user instance.
user.invitees #=> return a collection on user instances
User.rb
class User < ActiveRecord::Base
has_one :inviter, class_name: Invitation, foreign_key: :invitee_id
has_many :invitees, class_name: Invitation, foreign_key: :inviter_id
end
Invitation.rb
class Invitation < ActiveRecord::Base
belongs_to :inviter, class_name: User, foreign_key: :inviter_id
belongs_to :invitee, class_name: User, foreign_key: :invitee_id
end
migration
class CreateInvitations < ActiveRecord::Migration
def change
create_table :invitations do |t|
t.references :inviter, references: :user, index: true
t.references :invitee, references: :user, index: true
t.foreign_key :users, column: :inviter_id
t.foreign_key :users, column: :invitee_id
t.timestamps
end
end
end
This works half of the way because if I call user.inviter on a user that has an inviter it will return the invitation instance but not the user like I would like. Same for user.invitees returns a collection on invitation instances.
Do y'all have an idea of how to make it work ?
Your should use through option like this:
class User < ActiveRecord::Base
has_one :invitation, inverse_of: :inviter
has_one :inviter, through: :invitation
has_many :invitations, inverse_of: :invitee
has_many :invitees, through: :invitations
end
class Invitation < ActiveRecord::Base
belongs_to :inviter, class_name: User, inverse_of: :invitation
belongs_to :invitee, class_name: User, inverse_of: :invitations
end
user.invitees will give collection of invitation records.
Using IN query into User model with all invitee_id which reference to user model will give you collection of users.
user_ids = user.invitees.map(&:invitee_id)
User.where(id: user_ids)

Does anybody know another way to create join table migration and specify a specific name of references columns in join table?

I have these two models:
module Studying
class Student < ApplicationRecord
has_and_belongs_to_many :instructors,
class_name: 'Studying::Instructor',
foreign_key: 'studying_student_id',
association_foreign_key: 'studying_instructor_id'
end
end
module Studying
class Instructor < ApplicationRecord
has_and_belongs_to_many :students,
class_name: 'Studying::Student',
foreign_key: 'studying_instructor_id',
association_foreign_key: 'studying_student_id'
end
end
And for join_table I have generated migration:
def change
create_table :studying_instructors_students, id: false do |t|
t.belongs_to :studying_instructor, index: { name: 'index_instructors_students_on_studying_instructor_id' }
t.belongs_to :studying_student, index: { name: 'index_instructors_students_on_studying_student_id' }
end
end
So all is working fine, but the point is, that my senior comrade tells me that I should not use in models things such as:
foreign_key: 'studying_instructor_id',
and
association_foreign_key: 'studying_student_id'
but instead of these I should use:
foreign_key: 'instructor_id',
and
association_foreign_key: 'student_id'
and in the same way in the first model because this is against the convention.
I do not know how I can do this in this models and in tables (how you already understand tables names in db: studying_instructors and studying_students).
Any advice please ?
You can create relation table this way
def change
create_table :studying_instructors_students, id: false do |t|
t.integer :instructor_id, index: true
t.integer :student_id, index: true
end
end
and then you can use foreign_key: 'instructor_id', and association_foreign_key: 'student_id'
If I get this wrong way, please, feel free to address additional questions.
I would do something like this:
class Studying::Student < ApplicationRecord
has_many :student_instructors, class_name: 'Studying::StudentInstructor'
has_many :instructors, through: :student_instructors, class_name: 'Studying::Instructor'
end
class Studying::Instructor < ApplicationRecord
has_many :student_instructors, class_name: 'Studying::StudentInstructor'
has_many :students, through: :student_instructors, class_name: 'Studying::Instructor'
end
class Studying::StudentInstructor < ApplicationRecord
belongs_to :student, class_name: 'Studying::Student'
belongs_to :instructor, class_name: 'Studying::Instructor'
end
I have three tables here. One for students, second for instructors and third one is a junction table (student_instructors).
Migration for studnt_instructor will look something like this
def change
create_table :student_instuctors, id: false do |t|
t.integer :instructor_id, index: true
t.integer :student_id, index: true
end
end
I have not verified the syntax but this is more of logic explanation.
I hope this helps.

Rails has_and_belongs_to_many relations with two types of Users and one type of Table

I have a problem related with this association. A pasted code is better than any title:
table.rb
class Table < ActiveRecord::Base
has_and_belongs_to_many :clients, class_name: 'User'
has_and_belongs_to_many :managers, class_name: 'User'
end
user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :tables
end
migration - join table
class UsersToTable < ActiveRecord::Migration
def change
create_table :tables_users, id: false do |t|
t.references :user, as: :client
t.references :user, as: :manager
t.references :table
end
end
end
Problem
tab = Table.new
tab.save
tab.clients.create
tab.clients.create
tab.clients.create
tab.managers.create
tab.managers.size # == 4
tab.clients.size # == 4
When I creating associated Objects(Users) they all are linked to both clients and managers.
I want to be able to create them separately - When creating a client - only number of clients rise, when creating manager, only number of managers rise.
In other words I want this:
tab.managers.size # == 1
tab.clients.size # == 3
Could you please help?
has_and_belongs_to_many :stuff, class_name: 'StuffClass' is just DSL for:
has_many "<inferred_join_table_name>"
has_many :stuff, through: "<inferred_join_table_name>"
It seems that since clients and managers are names for Users, the inferred join table get's to be "TablesUsers", and that is not right.
Try specifyng the join table for both and using different join tables for each relationship:
class Table
has_many :tables_clients
has_many :clients, through: :tables_clients
has_many :tables_managers
has_many :clients, through: :tables_managers
end
class TablesClients
belongs_to :client, class_name: 'User'
belongs_to :table
end
create_table :tables_clients, id: false do |t|
t.references :client, index: true
t.references :table, index: true
end
# and the same for tables_managers
Then the user belongs to Tables in too different ways:
class User
has_many :client_tables_users, class_name: 'TablesUsers', foreign_key: :client_id
has_many :tables_as_client, through: :client_tables_users, source: :table
has_many :managed_tables_users, class_name: 'TablesUsers', foreign_key: :manager_id
has_many :managed_tables, through: :managed_tables_users, source: :table
end

Resources