我正在创建一个基本的订阅应用程序,我想知道最干净的方式来建模下面的关系。我似乎对has_and_belongs_to_many关系和何时使用它们感到困惑。
我试图大致创建下面的结构,并注意以下几点。subscription和order都只能有一个plan,但是subscriptions和orders都可以有很多products。
计划也是以与产品相同的方式创建的。也就是说,可以创建6个计划,然后将其添加到任意数量的订阅中(类似于产品)。
关系设计

目前,我有以下几点:
# User Model
has_one :subscription
has_one :plan, through: :subscription
# Subscription Model
belongs_to :user
belongs_to :plan # Not sure this is correct as a subscription should only be allowed to have 1 plan which belongs to the subscription.
# Plan Model
has_many :subscriptions # Again this doesn't feel quite right as I think the plan should belong to the subscription.
# Product Model
has_and_belongs_to_many :orders
has_and_belongs_to_many :subscriptions
# Order Model
belongs_to :user
has_and_belongs_to_many :products任何建议,人们可以提供最好的方式建模,这将是非常感谢的。
发布于 2017-03-23 09:40:48
在我看来,has_many :通过联想更合适。
basics.html#the-has-many-through-association
# User model
has_one :subscription
has_one :plan, through: :subscription
# Subscription model
belongs_to :user
belongs_to :plan
has_many :orders
# Plan (Subscription Plan) model
has_many :subscriptions
has_many :users, through: :subscriptions
# Order model
belongs_to :subscription你能详细介绍一下Order和Product的模型吗?你想让他们代表什么?如果Order表示订阅上的每个事务,那么它应该属于Subscription。
发布于 2017-03-23 13:54:00
如果您只需要处理@plans,我认为答案很简单--您可以让@subscriptions和@orders都属于@plans,每个@plans都可以定义为have_many。
# User model
has_one :subscription
has_many :orders
# Plan model
has_many :subscriptions
has_many :orders
# Subscription model
belongs_to :user
belongs_to :plan
# Order model
belongs_to :user
belongs_to :plan(我很欣赏你的观点,“感觉”就像一个计划应该属于订阅,而不是相反,但你必须‘使用力量,卢克’在这里一点)。
我认为在@products中折叠的方法可能是使用连接表:
# Order join table - OrderProduct
belongs_to :order
belongs_to :product
# Subscription join table - SubscriptionProduct
belongs_to :subscription
belongs_to :product然后,我想,其余的模型看起来是:
# User model
has_one :subscription
has_many :orders
# Plan model
has_many :subscriptions
has_many :orders
# Subscription model
belongs_to :user
belongs_to :plan
has_many :subscription_products
has_many :products, :through => :subscription_products
# Order model
belongs_to :user
belongs_to :plan
has_many :order_products
has_many :products, :through => :order_products
# Product model
has_many :subscription_products
has_many :subscriptions, :through => :subscription_products
has_many :order_products
has_many :orders, :through => :order_products创建一个新的@order可能如下所示:
@order = Order.create(user: @user, plan: @plan)
@order.products = @products
@order.save一个新的@subscription也是类似的。
如果您希望获得订阅给定@users的所有@product的列表,则可以:
@users = @product.subscriptions.map{|x| x.user}如果您希望看到当前订阅的所有@products:
@products = @user.subscription.products如果您想看到所有曾经订购过产品的@users:
@users = @product.orders.map{|x| x.user}.uniq(如果用户多次订购同一产品,.uniq)
发布于 2017-03-23 15:28:17
has_belongs_to_many指定了与另一个类的多到多关系,即不为联接表创建单独的类,我们可以在这些父类中直接指定。
class A < ActiveRecord::Base
has_many :abs
.....
end
class B < ActiveRecord::Base
has_many :abs
....
end
class AB < ActiveRecord::Base
belongs_to :a
belongs_to :b
....
end而不是这样,我们可以直接在类A和B中指定
class A < ActiveRecord::Base
has_belongs_to_many :as
....
end
class B < ActiveRecord::Base
has_belongs_to_many :bs
....
end希望现在对has_belongs_to_many协会已经很清楚了。
现在,对于模型设计,我们必须保留Order类,它特别指向订购带有订阅的产品的用户。
ie,orders表将充当与用户、产品、订阅相关联的连接表。
orders是一个指向orders模型的表
Rails协会将如下所示:
class User < ActiveRecord::Base
has_many :subscriptions
has_many :orders
has_many plans, through: :subscription
....
end
class Plan < ActiveRecord::Base
has_many :subscriptions
has_many :users, through: :subscriptions
....
end
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :plan
has_many :orders
....
end
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :product
belongs_to :subscription
....
end
class Product < ActiveRecord::Base
has_many :orders
has_many :users, through: :orders
....
end希望这对你有帮助:)。
https://stackoverflow.com/questions/42971426
复制相似问题