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
Related
I'm new to rails and I have this problem
I have three models, provider model, product model and categorie model, all models are related.
If you enter a product, you can select provider and categorie for this product.
I can not delete a provider or a categorie because they relate to product
But I can remove a product without problems
As I can manage to eliminate a provider or gategorie without affecting product?
class Categorie < ActiveRecord::Base
belongs_to :user
end
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :provider
belongs_to :categorie
end
class Provider < ActiveRecord::Base
belongs_to :user
has_many :products
end
Thank!
You can use dependent: destroy on your has_many relationships so that when a provider with many products is deleted, the products are deleted as well:
class Provider < ActiveRecord::Base
has_many :products, dependent: :destroy
end
Use this gem for deleting purpose. It will soft delete entities https://github.com/rubysherpas/paranoia
In my app I have 2 classes. User and Classroom. I use the user class as a student as well.
I'm trying to achieve a result where:
A classroom belongs to a user.
A user has many classrooms.
A classroom has one student through the user class.
A student can be associated to many classrooms.
To try and explain further. I have a classroom and the user is the creator of the classroom. When someone joins they are a student of the classroom and I only want there to be one student and one creator.
I want a student to be attached to lots of different classrooms and I want the classrooms to all belong to one user.
My current code for the two classes looks like this:
class User < ActiveRecord::Base
has_many :classrooms
end
class Classroom < ActiveRecord::Base
belongs_to :user
has_one :student, :class_name => "User"
end
Any advice is much appreciated. Thanks!
I think what you are trying to achieve is:
class User < ActiveRecord::Base
has_many :classroom_users
has_many :classrooms, through: :classroom_users
end
class ClassroomUser < ActiveRecord::Base
belongs_to :classroom
belongs_to :user
end
class Classroom < ActiveRecord::Base
has_many :classroom_users
has_many :users, through: :classroom_users
end
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
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
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.