Ruby Active Record Relational - ruby-on-rails

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

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

Ruby on rails 5 3 model associations

I have 3 models, User, Product, Coupon.
A User has many Products, Product belongs to User.
A User has many Coupons, Coupon belongs to User.
My goal is to apply a Coupon to a Product. A Product can have one coupon and a Coupon can be applied to many Products. Currently I have the models set up like this:
#coupon.rb
class Coupon < ApplicationRecord
belongs_to :user
has_many :products
validates_presence_of :code, :discount_percent, :description
end
#user.rb
class User < ApplicationRecord
has_many :products
has_many :coupons
end
#product.rb
class Product < ApplicationRecord
belongs_to :user
has_one :coupon, dependent: :destroy
end
Currently a user can successfully create a coupon, but if I apply the coupon to the product and try to delete the coupon, it gives me a foreign key error.
I've thought about making the product.coupon_id = nil inside the destroy action of the coupons_controller but I feel that is a bad practice. Ex.)
#coupons_controller.rb
def destroy
products = Product.where(coupon_id: #coupon.id)
products.each do |product|
product.coupon_id = nil
product.save
end
#coupon.destroy
end
I think I have something wrong with my associations but can't seem to figure it out! Using Postgres.
I appreciate any help!
class Coupon < ApplicationRecord
belongs_to :user
has_many :products, dependent: :nullify
validates_presence_of :code, :discount_percent, :description
end

Matching the records in ruby on rails

I have a model company, which has association, has many candidates, and belongs to company.
And I have another model key_skill, which has association, has many key_skills, and belongs to candidate.
Another model is candidate, which belongs to company, and has many key skills association.
I am trying to get the candidate whose key skills are matched to the required skill and, it should search and get the candidate who belongs to the particular company.
How can I write a query in the model for this situation?
These are the associations
company.rb
class Company < ActiveRecord::Base
has_many :candidates
end
candidate.rb
class Candidate < ActiveRecord::Base
belongs_to :company
has_many :key_skills, dependent: :destroy
accepts_nested_attributes_for :key_skills, reject_if: :all_blank,
allow_destroy: true
end
key_skill.rb
class KeySkill < ActiveRecord::Base
belongs_to :candidate
end
I think your current association condition is like this:
class Company < ApplicationRecord
has_many :candidates
end
class Candidate < ApplicationRecord
belongs_to :company
has_many :key_skills
end
class KeySkill < ApplicationRecord
belongs_to :candidate
end
For example to fetch all candidates with key_skills with ids 1,2,3 run the following query
Candidate.joins(:company, :key_skills).where("key_skills.id in (?)", [1,2,3])
Try the below:
I am assuming that key_skills table have a field skill and you want to perform search on it.
candidate = Candidate.includes(:company, :key_skills).where("key_skills.skill like ?", "%#{params[:skill]}%")
company = candidate.company

Rails model has and belongs to many list types

I have two models, User and Product
class User < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :users
end
Now I want to make three lists for each user, products they've checked out recently, products that are in their shopping cart and products which they have bought. How do I make a distinction between these relations? Could I add some sort of type column to the relation table? And how could I then later check this type?
Thanks
If you and to add other columns to the relation table perhaps you should consider using has_many on both User and Products, then you can add your columns on Cart
class User < ActiveRecord::Base
has_many :carts
has_many :products, through: :carts
end
class Cart < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :carts
has_many :users, through: :carts
end

When should my model belong to another one?

I'm unsure if I can accomplish what I want with my associations.
This is the scenario I want in my application:
A user will select a Store. Then inside of that Store a user selects a product and then can add a new price to that product.
class Business < ActiveRecord::Base
has_many :stores
end
class Store < ActiveRecord::Base
belongs_to :business
has_many :prices
end
class User < ActiveRecord::Base
has_many :prices, :dependent => :destroy
has_many :products, :through => :prices
end
class Price < ActiveRecord::Base
belongs_to :user
belongs_to :product
belongs_to :store
end
class Product < ActiveRecord::Base
has_many :prices
has_many :users, :through => :prices
end
I'm not sure if this is correct since products don't belong to a store (integer store_id in table).
What do I need to do to make this scenario work out? Is this the right design?
This is a good and correct design for what you are trying to do.
If a product belonged to a store, then a product could only be at one store. Since store may have many products and products may be sold at many stores, you need another table referencing both the product and store.

Resources