ActiveRecord association through multiple tables - ruby-on-rails

I have the following schema for my Rails app :
A project has many reviews, each of those reviews is filled out by a unique User to calculate a global score.
We could say that "An Organization of Users handles many Entities that have many Projects which are reviewed by the Users".
As you can see, I have a circular reference since I linked the tables "Users" & "Reviews".
After many tries and many search, I just can't manage to associate a User to a Review...Here is what I did so far:
1. Creation of an association table "UserReviews"
2. Model User
class User < ApplicationRecord
belongs_to :organization
####
has_many :user_reviews
has_many :reviews, through: :user_reviews
end
3. Model Review
class Review < ApplicationRecord
belongs_to :project
##
belongs_to :user_reviews
has_one :user, through: :user_reviews
end
4. Model UserReview
class UserReview < ApplicationRecord
belongs_to :user
belongs_to :review
end
I want to be able to get user.reviews or review.user.
Basically...I have to admit I'm lost despite the documentation. I never had to deal with this kind of issue.
Many thanks for your help!

Why do you need UserReview model here? I suppose Review model suffices your use case.
Change the Review model to:
class Review < ApplicationRecord
belongs_to :project
belongs_to :user
end

Related

Model for an eCommerce shop

I'm trying to create an eCommerce shop. My main idea is every user can create their own store, and I have models like this:
model/user.rb:
class User < ApplicationRecord
has_one :store
end
model/store.rb:
class Store < ApplicationRecord
belongs_to :user
has_many :products
end
model/product.rb
class Product < ApplicationRecord
belongs_to :store
end
I don't know if the relationships between these models is good enough or I should modify them. And for further update, if user_1 add product sold by user_2 to his cart, I don't know what will the relationship be between these two users
Your models don't have a relationship between User and Product yet.
You can say User has_many: :products
and Product has_one: :user but this relationship would be incomplete.
The user-product relationship needs to be done through the store. That's where has_many: through comes in handy.
User has_many :products, through: :store
Product has_one :user, through: :store

Rails modeling ActiveRecord associations of Chatrooms & Conversation with multiple users

I'm uncertain of the best approach to model conversation relationships.
My application has Users, Conversations, UserConversations, Venues, & VenueConversations.
Users can have many Conversations & Conversations can have many Users. Thus, the bridging table (UserConversations).
Venues can also have Conversations and these Conversations can also have many Users. Thus, the bridging table (VenueConversations)
I've tried it with polymorphic associations but then Messages in the Conversations became difficult to manage. Also, a while back I asked a question on Stack and someone said I should steer clear of polymorphic associations at all costs.
I hope to do this properly so that querying doesn't become a nightmare in the future.
I'd also like to state I'd like to have one Conversation model because one day they'll share a lot of behavior.
User.rb
class User < ApplicationRecord
has_many :user_conversations
has_many :conversations, through: :user_conversations
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
UserConversation.rb
class UserConversation < ApplicationRecord
belongs_to :user
belongs_to :conversation
end
Venue.rb
class Venue < ApplicationRecord
has_many :venue_conversations
has_many :conversations, through: :venue_conversations
end
VenueConversations.rb
class VenueConversations < ApplicationRecord
belongs_to :user
belongs_to :venue
belongs_to :conversation
end
The problem I'm having now is that when I do User.first.conversations I only find one of the types of conversations a User could have(Private vs Venue). It reflects the last definition I gave to has_many :conversations in the User.rb model. Perhaps a scope is in order? How would I make a distinction between private & venue conversations as well?
Please advise me on how I could resolve this or take a better approach

Ruby Active Record Relational

Still a newbie here, but I still couldn't get the logic right.
Currently, I have:
User has many products.
Product has 1 user with a price attribute.
I am trying to add on:
User can offer 1 price on a product sold by another user. User can offer price on multiple products.
A Product can have many offered price by multiple users.
I have currently come out with:
class User < ActiveRecord::Base
has_many :products
has_many :offered_prices
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :offered_prices
end
This is what I have done so far. It still doesn't seem quite right as I am rather confused at the same time. Your help is very much appreciated! :)
Define three models:
User | OfferedPrice | Product
The association amongst them will be:
class User < ActiveRecord::Base
has_many :products
has_many :offered_prices, through: :products
end
class OfferedPrice < ActiveRecord::Base
belongs_to :user
belongs_to :product
# To make sure a user can offer price once for against a product
validates_uniqueness_of :price, scope: [:user, :product]
end
class Product < ActiveRecord::Base
has_many :offered_prices
has_many :user, through: :offered_prices
end

Rails: accessing associated models with several degrees of separation

I am a beginner in rails, and I have a question about accessing one model from another model that is associated with several degrees of separation.
Let's say I have these models:
Account has_many Spies
Spy has many SpyRelationships and belongs to Account
SpyRelationship belongs to Listing and belongs to Spy
How would I set up the associations so that I could simply pull all the listings associated with a given Account (via its spies and spyrelationships)? What line of code would allow me to do so, after those associations are setup properly?
I'm guessing you want to access a listing through a spy?
class Account < ActiveRecord::Base
has_many :spies
has_many :listings, through: :spies
end
class Spy < ActiveRecord::Base
belongs_to :account
has_many :spy_relationships
has_many :listings, through: :spy_relationships
end
class SpyRelationship < ActiveRecord::Base
belongs_to :listing
belongs_to :spy
end
class Listing < ActiveRecord::Base
has_many :spy_relationships
end

Rails Many-to-Many Relantionship Dilemma

I have a User model:
class User < ActiveRecord::Base
has_many :projects, dependent: :destroy
end
and a Project model:
class Project < ActiveRecord::Base
belongs_to :user
end
What should I do if I want a User to be able to fund Projects, and a Project can be funded by many Users?
This would mean I get a Many-to-many relationship, and I would need an additional intermediate table. Call it user_projects:
class UserProject < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
But how do I cope with the previous relationship I had between the models before I implemented the third one?
How do I know which project belongs to which user if I have the intermediate table?
Would I modify the tables the following way?
class User < ActiveRecord::Base
has_many :projects, through: :user_project, dependent: :destroy
has_many :user_projects
end
class Project < ActiveRecord::Base
has_many :user_projects
has_many :users, through: user_project
end
Whether or not you need an intermediate table depends on if there is any associated data you need to store with the user/project pair -- such as a dollar amount or date info or role, etc.
If you don't need to store anything else, then just use a HABTM relationship. Otherwise, your final solution would be the way to go.
I personally don't like the choice of 'user_project' as it's too close to the HABTM's 'users_projects'. Perhaps something like ProjectMember or ProjectFunder or Funding would be better, but it kind of depends on what extra data you need to store.

Resources