Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails中帐户和计费计划之间的关系_Ruby On Rails_Ruby_Model_Relationships - Fatal编程技术网

Ruby on rails RubyonRails中帐户和计费计划之间的关系

Ruby on rails RubyonRails中帐户和计费计划之间的关系,ruby-on-rails,ruby,model,relationships,Ruby On Rails,Ruby,Model,Relationships,我正在为我们的客户构建一个后端管理面板 我正在集成一个功能,允许用户升级和降级他们的每月订阅,这意味着为billing_plans表添加一个新模型 我一直在努力正确处理帐户和计划之间的关系 我有一个计费计划模型: class BillingPlan < ActiveRecord::Base self.table_name = "billing_plans" has_many :accounts, primary_key: 'name', foreign_key: 'audio_bi

我正在为我们的客户构建一个后端管理面板

我正在集成一个功能,允许用户升级和降级他们的每月订阅,这意味着为billing_plans表添加一个新模型

我一直在努力正确处理帐户和计划之间的关系

我有一个计费计划模型:

class BillingPlan < ActiveRecord::Base
  self.table_name = "billing_plans"
  has_many :accounts, primary_key: 'name', foreign_key: 'audio_billing_model'
end

我相信这可能会帮助其他人,而且我很确定以前一定有人遇到过它。

既然您试图建立一个
has\u many
属于
关系,只需在
has\u many
模型上定义主键,然后指示
所属\u to
模型将该主键用作其外键:

# app/models/billing_plan.rb
class BillingPlan < ActiveRecord::Base
    self.table_name = "billing_plans" # Seems unnecessary, as the table name by default is `billing_plans`
    has_many :accounts, primary_key: 'name'
end

# app/models/account.rb
class Account < ActiveRecord::Base # Remember to subclass `ActiveRecord::Base`
  has_many :contacts
  belongs_to :user, primary_key: :email, foreign_key: :billing_email_address
  has_one :billing_plan, foreign_key: 'name'
end
#app/models/billing_plan.rb
类BillingPlan
你说你想要一个账户和一个计划之间的关系,但是在你的代码中,你已经有了一个关系–一个
计费计划
有很多:账户
,一个
账户
有一个:计费计划
。您的问题是您不确定这是否是正确的关系吗?是的,我不确定这是否是正确的关系,因为它没有产生预期的效果。本页描述了六种类型的关系:谢谢,尽管您所描述的代码中“子类ActiveRecord::Base”是什么意思,您声明了
Account
类,但没有继承
ActiveRecord::Base
。然而,在您的类装饰中,您需要将
Account
声明为
ActiveRecord::Base
子类,就像我在代码中所做的那样。啊,我忘了复制和粘贴它。非常感谢。
# app/models/billing_plan.rb
class BillingPlan < ActiveRecord::Base
    self.table_name = "billing_plans" # Seems unnecessary, as the table name by default is `billing_plans`
    has_many :accounts, primary_key: 'name'
end

# app/models/account.rb
class Account < ActiveRecord::Base # Remember to subclass `ActiveRecord::Base`
  has_many :contacts
  belongs_to :user, primary_key: :email, foreign_key: :billing_email_address
  has_one :billing_plan, foreign_key: 'name'
end