Rails - db logic, scope data by company - ruby-on-rails

I have 3 models: User, Receipt and Article.
Only association is: Receipt has_many Articles.
Now I need 4. model, which is Company. User belongs_to company, and Company has_many Users.
User needs to see only Receipts and Articles that are in his Company.
Do I need to have company_id in User, Receipts and Articles and browse by that id, or there is a better way?

User needs to see only Receipts and Articles that are in his Company
Tweak your associations like so
#app/models/user.rb
class User < ApplicationRecord
belongs_to :company
end
#app/models/company.rb
class Company < ApplicationRecord
has_many :users
has_many :receipts
has_many :articles, through: :receipts
end
#app/models/receipt.rb
class Receipt < ApplicationRecord
belongs_to :company
has_many :articles
end
#app/models/article.rb
class Article < ApplicationRecord
belongs_to :receipt
end
Now you can call #user.company.receipts and #user.company.articles
Do I need to have company_id in User, Receipts and Articles and browse
by that id
With the above approach, you need have company_id in users and receipts.

You definitely need to have company_id in User unless you are going with multitenancy. And you will also need to have company_id in Receipts but not in Article as it is already associated with the Receipt.
To access receipts you just need to do user.company.receipts

Create Relationship between two table
class Receipt < ApplicationRecord
belongs_to :company
end
class Company < ApplicationRecord
has_many :receipts
end
Get the Record in simple way using that the relationship in model
ex: user.company.receipt.first.article

Related

ActiveRecord association through multiple tables

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

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 Populate Existing Join Table

So I have two tables, a Customers table and a Companies table. I have also created an empty Employees table that I would like to use as a join table.
These are the associations I have: (I want customers to be associated with their respective company)
class Company < ApplicationRecord
has_many :employees
has_many :customers, :through => :employees
end
class Customer < ApplicationRecord
belongs_to :employees
end
class Employee < ApplicationRecord
belongs_to :customer
belongs_to :company
end
Where would be the best way to do this? In my Customer#new method in the controller? I read that I need to use <<, but I don't know how to approach that.
You need to use the concept of Inverse association here:
class Customer has_many :companies, :through => :employees
You could just try delegating the company call on customers to their employee:
customer.rb
delegate :company, to: :employee
Now whenever you ask a customer its company, it will ask its employee to handle it.

Understanding rails has_one has_many model associations

I'm trying to understand a rails model association and having trouble figuring out what association I need to use:
Here's my app Models
Company ---- Subscription ---- SubscriptionType
The SubscriptionType has a list of 3 different types of subscriptions and their associated price.
A Company has_one :subscription.
A Subscription will belong_to :company.
It also has other fields, such as trial_start_date, trial_end_date, charge_date, etc.
At first, I thought that Subscription has_one SubscriptionType and SubscriptionType has_many Subscriptions however that relationship doesn't seem to work in my subscription_spec.rb
it { should have_one(:subscription_type) }
But that gives me the following error, which indicates that this relationship won't work since I don't want to have tons of records in the SubscriptionType table:
Expected Subscription to have a has_one association called subscription_type (SubscriptionType does not have a subscription_id foreign key.)
Can someone help me wrap my head around this?
The difference between has_one vs belongs_to is all about where the foreign key lives.
Subscription has_one :subscription_type means that SubscriptionType has a subscription_id column and belongs to only one Subscription.
Subscription belongs_to :subscription_type means that Subscription has a subscription_type_id column and SubscriptionType can belong to multiple Subscriptions.
So to answer your question, the correct relationship here is
class Subscription < ApplicationRecord
belongs_to :subscription_type
end
class SubscriptionType < ApplicationRecord
has_many :subscriptions
end
You can set the associations like this:
class Company < ApplicationRecord
has_one :subscription
end
# subscriptions table should have columns company_id and subscription_type_id
class Subscription < ApplicationRecord
belongs_to :company
belongs_to :subscription_type
end
class SubscriptionType < ApplicationRecord
has_many :subscriptions
end
With this setup, the associated objects can be accessed as:
company = Company.find(1)
# to get subscription object with company_id: 1
company.subscription
# to get subscription_type object of the company's associated subscription
company.subscription.subscription_type
# to get all subscriptions of a particular subscription_type
SubscriptionType.last.subscriptions
Then, your subscription_spec.rb looks like:
it { should belong_to(:company) }
it { should belong_to(:subscription_type) }

Association for model - has_many :through

I have three models - Company, User and CompanyUser. The associations are as follows.
Company.rb
has_many :company_users
has_many :users, :through => :company_users
User.rb
has_many :company_users, :dependent => :destroy
belongs_to :company
CompanyUser.rb
belongs_to :company
belongs_to :user
For fetching current_user.company, what moddifications are to be made in the model association?
Any help would be appreciated.
It should be:
has_many :companies, through: :company_users
A has_many :through association is often used to set up a many-to-many
connection with another model. This association indicates that the
declaring model can be matched with zero or more instances of another
model by proceeding through a third model.
So if you are creating three models and making a has_many :through association I believe that User will have many Companies and Company will have many Users.
But if you need that the user belongs to only one company instead of creating the third model save the company_id in the users table itself.
Update:
Now as your scenario is A company can have may users and User belongs to a single company, you need two models: User and Company. Your User model should have an attribute company_id and then company_id should be saved in users table only. Then the associations as follows:
class User < ActiveRecord::Base
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :users
end
Then you can do current_user.company
You can get more information on associations in the RailsGuides
According to the associations you have taken,
user already have as association with the company through the Company User model, so user may have many companies according to your associations.
so,
class User < ActiveRecord::Base
has_many :company_users, :dependent => :destroy
has_many :companies, :through => :company_users
end
current_user.companies will give you the companies.
But if you need only one company for a user then,
class User < ActiveRecord::Base
belongs_to :company
end
take belongs_to company and save company_id in users table,
then you can call,
`current_user.company`
According to your logic,
I think you may need to create a new variable session current_company_user which is object CompanyUser.
And then, to fetch company by :
current_company_user.company

Resources