首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails建模关系

rails建模关系
EN

Stack Overflow用户
提问于 2017-03-23 09:04:27
回答 3查看 83关注 0票数 0

我正在创建一个基本的订阅应用程序,我想知道最干净的方式来建模下面的关系。我似乎对has_and_belongs_to_many关系和何时使用它们感到困惑。

我试图大致创建下面的结构,并注意以下几点。subscriptionorder都只能有一个plan,但是subscriptionsorders都可以有很多products

计划也是以与产品相同的方式创建的。也就是说,可以创建6个计划,然后将其添加到任意数量的订阅中(类似于产品)。

关系设计

目前,我有以下几点:

代码语言:javascript
复制
# 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

任何建议,人们可以提供最好的方式建模,这将是非常感谢的。

EN

回答 3

Stack Overflow用户

发布于 2017-03-23 09:40:48

在我看来,has_many :通过联想更合适。

basics.html#the-has-many-through-association

代码语言:javascript
复制
# 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

你能详细介绍一下OrderProduct的模型吗?你想让他们代表什么?如果Order表示订阅上的每个事务,那么它应该属于Subscription

票数 0
EN

Stack Overflow用户

发布于 2017-03-23 13:54:00

如果您只需要处理@plans,我认为答案很简单--您可以让@subscriptions@orders都属于@plans,每个@plans都可以定义为have_many

代码语言:javascript
复制
# 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中折叠的方法可能是使用连接表:

代码语言:javascript
复制
 # Order join table - OrderProduct
 belongs_to :order
 belongs_to :product

 # Subscription join table - SubscriptionProduct
 belongs_to :subscription
 belongs_to :product

然后,我想,其余的模型看起来是:

代码语言:javascript
复制
# 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可能如下所示:

代码语言:javascript
复制
 @order = Order.create(user: @user, plan: @plan)
 @order.products = @products
 @order.save

一个新的@subscription也是类似的。

如果您希望获得订阅给定@users的所有@product的列表,则可以:

代码语言:javascript
复制
@users = @product.subscriptions.map{|x| x.user}

如果您希望看到当前订阅的所有@products

代码语言:javascript
复制
@products = @user.subscription.products

如果您想看到所有曾经订购过产品的@users

代码语言:javascript
复制
@users = @product.orders.map{|x| x.user}.uniq

(如果用户多次订购同一产品,.uniq)

票数 0
EN

Stack Overflow用户

发布于 2017-03-23 15:28:17

has_belongs_to_many指定了与另一个类的多到多关系,即不为联接表创建单独的类,我们可以在这些父类中直接指定。

代码语言:javascript
复制
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中指定

代码语言:javascript
复制
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协会将如下所示:

代码语言:javascript
复制
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

希望这对你有帮助:)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42971426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档