class QuestionSet
has_and_belongs_to_many :questions,
class_name: 'Exam',
join_table: 'question_question_sets',
foreign_key: 'question_set_id',
association_foreign_key: 'question_id'
end
class Question
has_and_belongs_to_many :question_sets,
class_name: 'Exam',
join_table: 'question_question_sets',
foreign_key: 'question_id',
association_foreign_key: 'question_set_id'
end
The above models are inherited from the base model Exam(using rails STI) and the join table contains two fields: question_id and question_set_id. Now I need to convert this association into has_many through.
I have tried as follows:
class QuestionQuestionSet
has_many :questions
has_many :question_sets
end
class Question
has_many :question_question_sets, foreign_key: :question_id
has_many :question_sets, through: :question_question_sets
end
class QuestionSet
has_many :question_question_sets, foreign_key: :question_set_id
has_many :questions, through: :question_question_sets
end
Even after editing your models, it's necessary to create a new join table since the previous one (created by habtm) doesn't have and "id" column.
As a reference you can follow the steps indicated in http://www.chrisrolle.com/en/blog/migration-path-from-habtm-to-has_many-through
Related
I'm trying to create a Referral program on a Rails app and I struggle with the relationships.
My Referral model is pretty simple : godfather_id, godson_id, state
Both godfather and godson ids references an User, which can have many godsons but only one godfather.
class Referral < ApplicationRecord
belongs_to :user
belongs_to :godson, class_name: 'User'
end
The issue comes in my User model. I wan't to be able to do user.godsons to get an array of godsons Users and user.godfather to get the godfather User.
I tried a few things and I think those two where the closest to what I need to do (User model simplified for the example).
class User < ApplicationRecord
has_many :referrals
has_many :godson, -> { where(godfather_id: id) }, through: :referrals
has_one :godfather, -> { where(godson_id: id) }, through: :referrals
end
class User < ApplicationRecord
has_many :godson_relations, class_name: 'Referral', foreign_key: 'godson_id'
has_many :godsons, through: :godson_relations
has_one :godfather_relation, class_name: 'Referral', foreign_key: 'godfather_id'
has_one :godfather, through: :godfather_relations
end
I'm really unsure about how to materialize this relationship, any help will be appreciated 🙏
To make an actual self-referential assocation you would just add a column on the users table that points back to the same table:
class AddGodfatherToUsers < ActiveRecord::Migration[6.1]
def change
add_reference :users, :godfather, null: true,
foreign_key: { to_table: :users }
end end
class User
belongs_to :god_father,
class_name: 'User',
optional: true,
inverse_of: :god_children
has_many :god_children,
class_name: 'User',
foreign_key: :god_father_id
inverse_of: :god_father
end
If you must store Referalls as a separate table you were kind of on the right track but you got the foreign keys backwards:
class Referral < ApplicationRecord
# you better be explicit here or its going to get extremely confusing
belongs_to :godfather, class_name: 'User'
belongs_to :godson, class_name: 'User'
end
class User < ApplicationRecord
has_many :referrals_as_godfather,
class_name: 'Referral', foreign_key: 'godfather_id'
has_one :referral_as_godson,
class_name: 'Referral',
foreign_key: 'godfather_id'
has_many :godsons, through: :referrals_as_godfather
has_one :godfather, through: :referral_as_godson
end
It should be noted that has_one in no way guarentees that a user can have only one referral (and thus one godfather). It just adds a LIMIT 1 to the query. You would have to enforce that with a uniqueness constraint and validations.
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.
I have one user model and one viewed_contractor model. I am treating user model as customer and contractor. customer can view many contractors by visiting their respective profile.Contractor can be viewed by many customers. I have customer_id and contractor_id in my viewed_contractor. I want to handle this relation as has_many through. Is it possible thorough has_many through?
It is possible. First, you'd need to specify the class_name option for the belongs_to associations in your ViewedContractor model so that they both refer to your User class. Then you could specify the has_many through: relations in your User model.
Something like this should work:
# viewed_contractor.rb
class ViewedContractor < ActiveRecord::Base
belongs_to :contractor, class_name: 'User', foreign_key: :contractor_id
belongs_to :customer, class_name: 'User', foreign_key: :customer_id
end
# user.rb
class User < ActiveRecord::Base
has_many :viewed_contractors_as_contractor, class_name: 'ViewedContractor', foreign_key: :contractor_id
has_many :viewed_contractors_as_customer, class_name: 'ViewedContractor', foreign_key: :customer_id
has_many :visited_contractors, through: :viewed_contractors_as_customer, source: :contractor
has_many :visited_customers, through: :viewed_contractors_as_contractor, source: :customer
end
I'm trying to create a join table between 2 models that has 3 columns. The models are called User and Dare. The join table is called DaresUsers. And what I want to include in the join table is an author_id, accepter_id, dare_id.
Each dare will only have one author but will have many accepters because more than one user can accept the same dare. Should I use a has_many through relationship and if so what would I declare in my models? My confusion here is because the User model is referenced by the join table in two respects: the author_id and accepter_id.
Thanks for any help!
Try this:
class Dare < ActiveRecord::Base
belongs_to :author, class_name: 'User'
has_many :dare_user_relations, dependent: :destroy
has_many :accepters, through: :dare_user_relations
end
class DareUserRelation < ActiveRecord::Base
belongs_to :accepter, class_name: 'User'
belongs_to :dare
end
class User < ActiveRecord::Base
has_one :dare, foreign_key: 'author_id', dependent: :destroy
has_many :dare_user_relations, dependent: :destroy
has_many :dares, through: :dare_user_relations, foreign_key: 'accepter_id'
end
or w/o a model:
class Dare < ActiveRecord::Base
belongs_to :author, class_name: 'User'
has_and_belongs_to_many :accepters, class_name: 'User', association_foreign_key: 'accepter_id'
end
class User < ActiveRecord::Base
has_one :dare, foreign_key: 'author_id', dependent: :destroy
has_and_belongs_to_many :dares, foreign_key: 'accepter_id'
end
I have those two Models, in a HABTM Relationship:
The Project is using Rails 4, so no attr_accessible tags
wine.rb
class Wine < ActiveRecord::Base
has_and_belongs_to_many :pairings, class_name: 'Food', join_table: 'foods_wines', association_foreign_key: 'food_id'
has_many :images, as: :attachable, class_name: 'Asset', dependent: :delete_all
end
food.rb
class Food < ActiveRecord::Base
has_and_belongs_to_many :wines, class_name: "Wine", join_table: "foods_wines", foreign_key: "food_id"
end
I created the Join Table with this migration:
create_table(:foods_wines, :id => false) do |t|
t.integer :food_id
t.integer :wine_id
end
add_index :foods_wines, [:food_id, :wine_id]
When I try to create the new Relation in the Rails Console, it does not seem to be saving the HABTM Relationship.
#wine.pairings.create(:name => "Seafood")
it does not seem to be saving the HABTM Relation -> When I restart the console, the relation is gone - I also checked inside the DB, where I get an empty table for the foods_wines table.
Am I missing something crucial here?
I think, you have to replace :
has_and_belongs_to_many :pairings, class_name: 'Food', join_table: 'foods_wines', association_foreign_key: 'food_id'
with :
has_and_belongs_to_many :pairings, class_name: 'Food', join_table: 'foods_wines', foreign_key: 'wine_id'
in wine.rb, because you have to specify the foreign key of this class (Wine).