同一模块中的Rails关系会抛出未初始化的常量错误

时间:2015-07-16 04:01:29

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord

我有几个看起来像这样的课程:

app.post('/views/adminpanel/:url',auth.requiresLogin, users.geturl);

exports.geturl= function(req,res)
{
    var url = req.body.url1;
    res.render(url);

}

这似乎应该有效,但显然不是:

module Billing
  class Subscription < ActiveRecord::Base
    belongs_to :pricing_plan
  end
end

module Billing
  class PricingPlan < ActiveRecord::Base
    has_many :subscriptions
  end
end

我尝试在关系中添加一个显式:class_name:

2.1.2 :001 > Billing::Subscription.first.pricing_plan
  Billing::Subscription Load (0.2ms)  SELECT  `billing_subscriptions`.* FROM `billing_subscriptions`   ORDER BY `billing_subscriptions`.`id` ASC LIMIT 1
NameError: uninitialized constant Billing::Subscription::PricingPlan

但这让我无处可去:

has_many :subscriptions, :class_name => 'Billing::Subscription'

belongs_to :pricing_plan, :class_name => 'Billing::PricingPlan'

这里发生了什么以及如何从这个烂摊子中解脱出来?

1 个答案:

答案 0 :(得分:1)

查看http://dan.chak.org/enterprise-rails/chapter-3-organizing-with-modules/中的示例代码,您可以尝试:

has_many :subscriptions, :class_name => '::Billing::Subscription'

belongs_to :pricing_plan, :class_name => '::Billing::PricingPlan'

请注意::之前的前导Billing