Ruby on rails RubyonRails建模关系

Ruby on rails RubyonRails建模关系,ruby-on-rails,ruby,activerecord,model,relationships,Ruby On Rails,Ruby,Activerecord,Model,Relationships,我正在创建一个基本的订阅应用程序,我想知道建立以下关系模型的最干净的方法是什么。我似乎对has\u和\u属于\u许多关系以及何时使用它们感到困惑 我试图大致创建以下结构,需要注意的几点如下。订阅和订单只能有一个计划,但订阅和订单都可以有许多产品 计划的创建方式也与产品相同。i、 e.6可以创建计划,然后将其添加到任意数量的订阅中(类似于产品) 关系设计 目前我有以下几点: # User Model has_one :subscription has_one :plan, through: :s

我正在创建一个基本的订阅应用程序,我想知道建立以下关系模型的最干净的方法是什么。我似乎对
has\u和\u属于\u许多
关系以及何时使用它们感到困惑

我试图大致创建以下结构,需要注意的几点如下。
订阅
订单
只能有一个
计划
,但
订阅
订单
都可以有许多
产品

计划的创建方式也与产品相同。i、 e.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

如果有人能提供任何关于建立模型的最佳方法的建议,我们将不胜感激

在我看来有很多:通过交往更合适


您能否详细说明
订单
产品
型号?你想让他们代表什么?如果
订单
表示订阅上的每笔交易,那么它应该属于
订阅

如果你只需要处理
@计划
,我认为答案很简单-你可以拥有
@订阅
@订单
属于
@计划
,其中每一个都可以定义为有许多

# 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
新的
@订阅将类似

如果您想要订阅给定
@产品的所有
@用户的列表
,您可以:

@users = @product.subscriptions.map{|x| x.user}
如果要查看当前订阅的所有
@产品

@products = @user.subscription.products
如果要查看所有订购过产品的
@用户

@users = @product.orders.map{|x| x.user}.uniq

.uniq
以防用户多次订购同一产品)

has\u belish\u to\u 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

希望这对您有用:)。

在编写表定义时,通常很清楚您需要什么关系。例如,如果您将
用户id
添加到
订阅
中,则
订阅
属于
用户
。如果您无法将外来id添加到任何一个表中,则表示
拥有且属于许多
感谢您的回复。订单应该通过您提到的订阅来表示每笔交易。还值得一提的是,每个客户只能有一个订阅:)。再次感谢您的支持advice@TomPinchen好的,我相应地更新了我的答案。希望这有帮助:)
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
class A < ActiveRecord::Base
  has_belongs_to_many :as
  ....
end

class B < ActiveRecord::Base
  has_belongs_to_many :bs
  ....
end
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