I have a model for User and a model for Article.
So in the article model I want to have an owner, and a reviewer (2 different users).
Owner is easy - belongs_to :owner.
But what about reviewer?
Do I need to have an Integer field with the id of the reviewer document?
Do I use belongs_to again?
I'm a bit confused...
You can do this using the inverse_of keyword:
class User
has_many :articles, class_name: 'Article', inverse_of: :owner
has_many :reviews, class_name: 'Article', inverse_of: :reviewer
end
class Article
belongs_to :owner, class_name: 'User', inverse_of: :articles
belongs_to :reviewer, class_name: 'User', inverse_of: :reviews
end
This will tell mongoid what is the "inverse" relation name for you association.
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 a User model, and an Order model. There are two types of users: a seller, and a driver. Both sellers and drivers have many orders, and every order belongs to one seller and one driver. Thus, I want to be able to query for user.orders and have it return a particular user's orders, but also to successfully query for order.seller and order.driver. How can I establish these relationships?
This is what I came up with.. Pretty sure it works as intended.
Class User
has_many :sales, class_name: 'Order', inverse_of: :seller
has_many :pickups, class_name: 'Order', inverse_of: :driver
end
Class Order
belongs_to :seller, class_name: "User", inverse_of: :sale
belongs_to :driver, class_name: "User", inverse_of: :pickup
end
Seems like there is no clear way to have 2 relations share a field on another model. You could always do something like this if you have a field that differentiates drivers and sellers if they are a mix tho it won't work.
User.rb
has_and_belongs_to_many :orders
Order.rd
has_and_belongs_to_many :users
def seller
users.where(job: User.DRIVER)
end
UPDATE
User.rb
has_many :sales, class_name: 'Order', inverse_of: :seller
has_many :pickups, class_name: 'Order', inverse_of: :driver
def orders
Order.or(seller_id: self.id, driver_id: self.id)
end
Order.rb
belongs_to :seller, class_name: 'Order', inverse_of: :sales
belongs_to :driver, class_name: 'Order', inverse_of: :pickups
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 have three models
User
Product
Order
A user who submits products and other users can order that product.
Now I want to implement another model payment, where I as an admin pay the user.
Question
I am confused about what kind of association should i create between user, order and payment?
This is what I currently have :
in app/models/user.rb
class User < ActiveRecord::Base
has_many :products_selling, class_name: 'Product'
has_many :orders_received, class_name: 'Order', through: :products_selling, source: :orders
has_many :orders_made, class_name: 'Order'
has_many :products_ordering, class_name: 'Product', through: :orders_made, source: :product
has_one :payment, class_name: 'Payment', through: :orders_received
and in app/models/order.rb
class Order < ActiveRecord::Base
belongs_to :product
belongs_to :buyer, class_name: 'User', foreign_key: :user_id
has_one :seller, class_name: 'User', through: :product
has_one :payment, through: :seller
and in app/models/payment.rb
class Payment < ActiveRecord::Base
belongs_to :user
belongs_to :order
I am not sure what association should be used, I have been reading and there are examples using polymorphic: :true but they were all with has_many, where as in my case one order corresponds to one payment.
Associations
Polymorphic associations basically allow you to use a single table to manage multiple associations:
So if you wanted to link multiple tables to a single table, it will essentially create a pseudo object, which can be associated with different data types. We typically use polymorphic associations with tables which can be used by multiple models, examples including images, errors, posts etc:
In regards to relating polymorphic associations to has_many / has_one, I'm not entirely sure (I can research if you want)
--
Fix
In your case, I would do this:
#app/models/user.rb
class User < ActiveRecord::Base
has_many :orders
has_many :purchases, class_name: 'Order', foreign_key: "buyer_id"
has_many :products
has_many :bought, class_name: 'Product', through: :purchases
end
#app/models/order.rb
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :product
has_one :payment
end
#app/models/payment.rb
Class Payment < ActiveRecord::Base
#fields - id | user_id | order_id | created_at | updated_at
belongs_to :order #-> user pays for order
belongs_to :user #-> user making the payment
end
This will allow you to create payments for each order - which is really what payments are for, right? (You pay for the order, not the product)
I have self referential model called Profile that is connected through the Relationship model.
class Profile < ActiveRecord::Base
belongs_to :user
has_many :accepted_relationships, class_name: 'Relationship', foreign_key: 'responder_id'
has_many :followers, through: :accepted_relationships, source: 'initiator'
has_many :initiated_relationships, class_name: 'Relationship', foreign_key: 'initiator_id'
has_many :followed_profiles, through: :initiated_relationships, source: 'responder'
has_many :groups
end
class Relationship < ActiveRecord::Base
belongs_to :responder, class_name: 'Profile', foreign_key: 'responder_id'
belongs_to :initiator, class_name: 'Profile', foreign_key: 'initiator_id'
belongs_to :group
end
class Group < ActiveRecord::Base
belongs_to :profile
has_many :relationships
attr_accessible :name
end
The problem is I don't know how to access data on the join model. If I do something like;
user.profiles[1].followers[1]
it will give me the profile I want. I would also like to have something like;
user.profiles[1].followers[1].assigned_group
so I could access the group that the relationship belongs to.
Is my design off, or am I overlooking something here?
I guess, Your Group will have many profiles and does your group require relationships, you can have either relationships or profiles in your group or profiles through relationships
I'm getting closer to an answer. I am passing a block to has_many
has_many accepted_relationships... do
def assigned_group
#code
end
end
I still haven't quite gotten it, but I think this is the route I need to take.