Rails - has_many with different column names? - ruby-on-rails

I have a data model - SeasonTeams with the following relation:
has_many :linked_team_constraints, class_name: 'LinkedTeamConstraints', dependent: :destroy
Then I have the LinkedTeamConstraints model as follows:
belongs_to :season_team, primary_key: :_id
Now when I call destroy on the SeasonTeam - thereby destroying the LinkedTeamConstraint - I get error column linked_team_constraints.season_team_id does not exist. I do have two OTHER columns labeled season_team_id_1 and season_team_id_2. How can I establish a has_many relationship such that linked_team_constraints.season_team_id_1 or ..._id_2 is queried instead of ...season_team_id.
Would it be something like:
has_many :linked_team_constraints... {where("_id == ?", linked_team_constraints.season_team_id_1 || linked_team_constraints.season_team_id_2)}?

You need to specify foreign_key for your relations and have separate relation for each key. Like:
has_many :linked_team_1_constraints, class_name: 'LinkedTeamConstraints', foreign_key: :season_team_id_1, dependent: :destroy
has_many :linked_team_2_constraints, class_name: 'LinkedTeamConstraints', foreign_key: :season_team_id_2, dependent: :destroy

Related

has_many :through within same model

I have User model in my database. It has role. role can be either patient or doctor. I want to maintain doctor_patient table.
doctor_patient.rb
belongs_to :doctor, class_name: 'User'
belongs_to :patient, class_name: 'User'
a patient can belong to many doctors and a docor can have many patients. I am familier to regular or normal has_many through association but facing issues related to this scenarios where I have role in user model.
user.rb
user
has_many :doctor_patients
has_many :patients, :through => :doctor_patients, :class_name=> "User"
patient
has_many :doctor_patients
has_many :doctors, :through=> :doctor_patients, :class_name=> "User"
In ActiveRecord the assocation metadata (the reflection) is strored in a class attribute as a hash and the name is used as the key. So when you define multiple assocations with the same name you're just overwriting your previous assocation.
The solution is to use unique names for each assocation:
class User < ApplicationController
has_many :doctor_patients_as_doctor,
foreign_key: :doctor_id,
class_name: 'DoctorPatient'
has_many :patients,
through: :doctor_patients_as_doctor
has_many :doctor_patients_as_patient,
foreign_key: :patient_id,
class_name: 'DoctorPatient'
has_many :doctors,
through: :doctor_patients_as_patient
end
Also make sure you pluralize the table correctly and name it doctor_patients.

Multiple polymorphic associations on the same object

I have a Flag model which is joined to multiple other objects with FlagInstance and a polymorphic flaggable on that table:
table 'flag_instances'
flag_id
flaggable_id
flaggable_type
.....
With has many_through I'm able to fetch any flaggable object like user.flags which is great.
However I'm trying to flag objects with errors and the notify other objects so I've added
table 'flag_instances'
flag_id
flaggable_id
flaggable_type
notifiable_id
notifiable_type
.....
The problem is, a User can have a flag and can be notified of a flag. So user.flags isn't specific enough to show me which is a flag and which is a notification of a flag.
I think I need to change the relationships:
user.rb
has_many :flag_instances, as: :flaggable, dependent: :destroy
has_many :flags, through: :flag_instances
has_many :flag_instances, as: :notifiable, dependent: :destroy
has_many :flags, through: :flag_instances
But I'm not sure what to change them to. Can someone please suggest a solution?
Note: both flags and notifications of flags can belong to multiple objects, so they both need to remain polymorphic.
Thanks
Association for notifiable needs to be changed. In this case user.rb:
has_many :flag_instances, as: :flaggable, dependent: :destroy
has_many :flags, through: :flag_instances
has_many :notifiables, as: :notifiable, dependent: :destroy, class_name: 'FlagInstance'
has_many :notifications, through: :notifiables, class_name: 'Flag'
Note: You might also need to provide foreign_key in case Rails association is not able to pick up the key itself.
Each association must have a unique name - otherwise the later definition will just overwrite the former.
Here the third line overwrites the first line:
has_many :flag_instances, as: :flaggable, dependent: :destroy
has_many :flags, through: :flag_instances
has_many :flag_instances, as: :notifiable, dependent: :destroy
To reference the correct associations we would need to setup the user model as so:
class User < ApplicationRecord
has_many :flag_instances_as_flaggable,
as: :flaggable
class_name: 'FlagInstance'
has_many :flags_as_flaggable,
through: :flag_instances_as_flaggable,
source: :flag
has_many :flag_instances_as_notifiable,
as: :notifiable
class_name: 'FlagInstance'
has_many :flags_as_notifiable,
through: :flag_instances_as_notifiable,
source: :flag
end
In your case you might want to use concerns to keep it dry:
module Flaggable
extend ActiveSupport::Concern
included do
has_many :flag_instances_as_flaggable,
as: :flaggable,
class_name: 'FlagInstance'
has_many :flags_as_flaggable,
through: :flag_instances_as_flaggable,
source: :flag
end
end
module Notifiable
extend ActiveSupport::Concern
included do
has_many :flag_instances_as_notifiable,
as: :notifiable,
class_name: 'FlagInstance'
has_many :flags_as_notifiable,
through: :flag_instances_as_notifiable,
source: :flag
end
end
class User < ApplicationRecord
include Flaggable
include Notifiable
end

Rails AMS, specify different serializers in polymorphic list

I implemented a multi-table search using pg_search, and then serialize the result with Active Model Serializer (version 0.10) - it works fine, but AMS uses the default serializer for each of the types returned.
Here's the serializer:
class SearchBarSerializer < ApplicationSerializer
attributes :searchable_type
belongs_to :searchable
end
Thus, for example, when serializing the returned objects from pg_search, if the relevant object is a "User", then AMS uses UserSerializer. If the relevant type is a league, then AMS uses LeagueSerializer.
That's fine - but I would like to use a different serializer for each type. This is for a search bar, and so I only care about a much smaller amount of data than the full standard serializer. (EDIT: the standard serializers serialize all attributes and associations for each of the User and League models, which can be seen below. Each model is somewhat significantly large, and for the purposes of just a search, I really only need each model's name and id, and perhaps some other smaller data for each type)
Is there some way that I can specify which serializer to use depending on the object?
Thank you!
EDIT:
User Model:
class User < ActiveRecord::Base
include PgSearch
#################### Associations
has_and_belongs_to_many :roles
belongs_to :profile_page_visibility, optional: true # the optional part is just for when user's are created.
has_and_belongs_to_many :leagues, class_name: "Leagues::League", join_table: "users_leagues_leagues"
has_one :customer, class_name: "Payments::Customer", dependent: :destroy
has_many :unpaid_charges, class_name: "Payments::UnpaidCharge", dependent: :destroy
has_many :charges, class_name: "Payments::Charge", dependent: :destroy
has_many :cards, class_name: "Payments::Card", dependent: :destroy
has_many :league_join_requests, class_name: "Leagues::JoinRequest", dependent: :destroy
has_many :notifications, class_name: "Notification", foreign_key: :recipient_id
has_many :league_invitations, class_name: "Leagues::Invitation", dependent: :destroy
has_many :teams, class_name: "Leagues::Team"
has_many :divisions, class_name: "Leagues::Division" # Can act as division commissioner
has_many :conferences, class_name: "Leagues::Conference" # Can act as conference commissioner
League Model:
class Leagues::League < ApplicationRecord
enum pay_level: [ :basic, :custom, :premium ]
include PgSearch
#################### Associations
has_and_belongs_to_many :users, class_name: "User", join_table: "users_leagues_leagues"
has_and_belongs_to_many :commissioners, class_name: "User", join_table: "commissioners_leagues_leagues"
belongs_to :commissioner, class_name: "User", foreign_key: :commissioner_id, optional: true
has_and_belongs_to_many :feature_requests, class_name: "FeatureRequest", join_table: "feature_requests_leagues_leagues"
has_many :join_requests, class_name: "Leagues::JoinRequest", dependent: :destroy
has_many :invitations, class_name: "Leagues::Invitation", dependent: :destroy
has_many :notifications, class_name: "Notification", as: :notifiable_subject, dependent: :destroy
has_many :teams, class_name: "Leagues::Team", dependent: :destroy
has_many :conferences, class_name: "Leagues::Conference", dependent: :destroy
has_many :divisions, class_name: "Leagues::Division", dependent: :destroy
If you want to define a specific serializer lookup for your associations, you can override the ActiveModel::Serializer.serializer_for method to return a serializer class based on defined conditions.
For your case, it might look something like:
class SearchBarSerializer < ApplicationSerializer
attributes :searchable_type
belongs_to :searchable
class << self
def serializer_for(model, options)
return TinyUserSerializer if model.class == User
return TinyLeagueSerializer if model.class == Leagues::League
super
end
end
end

has_one through association with condition

I have 3 relevant tables/models. I would like to retrieve the user which has organized a party, given a Party record, a join table with a boolean organized column, and a users table.
My best attempt so far (this one makes the most sense to me after much fiddling). I've omitted the irrelevant columns.
class Party
# party_id
has_many :parties_users
has_many :users, through: :parties_users, source: :user
has_one :organizer,
-> { where organizer: true },
through: :parties_users,
source: :user
class PartiesUser
# party_id
# user_id
# organized:bool
belongs_to :party
belongs_to :user
class User
# user_id
has_many : parties_users
The above setup raises the following error, which I honestly don't fully understand:
ActiveRecord::HasOneThroughCantAssociateThroughCollection (Cannot have a has_one :through association 'Party#organizer' where the :through association 'Party#parties_users' is a collection. Specify a has_one or belongs_to association in the :through option instead.)
I know I can do this via an instance method, but given the frequency types of use, my app would massively benefit from having this as an association.
As the error message says, you can't have a has_one through a has_many.
You could (instead) do it this way if the organizer flag is on the join record...
has_many :parties_users
has_many :users, through: :parties_users, source: :user
has_one :main_party_user, -> {where organizer: true}, class_name: 'PartiesUser'
has_one :organizer, through: :main_party_user, class_name: 'User'

Get attribute value from the join in a many-to-many relationship

I have a many-to-many relation between User and "Link".
The join model is called LinkAddress and besides for saving the IDs of the other two models, it has an attribute called address - information it collects at creation.
How can I access the address attribute for a certain link in a request scenario like the following: User.first.links.first.address ?
Models:
class User < ActiveRecord::Base
has_many :link_addresses, dependent: :destroy
has_many :links, through: :link_addresses
accepts_nested_attributes_for :link_addresses, allow_destroy: true
end
class LinkAddress < ActiveRecord::Base
belongs_to :user
belongs_to :link
end
class Link < ActiveRecord::Base
has_many :link_addresses, dependent: :destroy
has_many :users, through: :link_addresses
end
You could access it through User since it's a has_many ... :through relation:
User.first.link_addresses.first.address
Or, if you'd like to go through links then:
User.first.links.first.link_addresses.first.address
SQL Aliases
I had this exact question: Rails Scoping For has_many :through To Access Extra Data
Here's the answer I got:
#Images
has_many :image_messages, :class_name => 'ImageMessage'
has_many :images, -> { select("#{Image.table_name}.*, #{ImageMessage.table_name}.caption AS caption") }, :class_name => 'Image', :through => :image_messages, dependent: :destroy
This uses SQL Aliases which I found at this RailsCast (at around 6:40 in). It allows us to call #user.image.caption (even though .caption is in the join model)
Your Code
For your query, I'd use this:
class User < ActiveRecord::Base
has_many :link_addresses, dependent: :destroy
has_many :links, -> { select("#{Link.table_name}.*, #{LinkAddress.table_name}.address AS address") }, through: :link_addresses
accepts_nested_attributes_for :link_addresses, allow_destroy: true
end
This will allow you to write #user.links.first.address, and gracefully handles an absence of the address record

Resources