如何引用深层关联中的属性

时间:2013-08-19 19:56:09

标签: ruby-on-rails ruby-on-rails-3.2 associations erb refinerycms

摘要

使用RefineryCMS 2.0的Rails 3.2。这些是我的伪代码模型:

Industry
  name
  has_many companies
  has_many works through companies

Company 
  name
  has_many works
  belongs_to industry

Work
  name
  belongs to company

从Work的一个实例中,我可以说work.company.name并获取相关公司的名称。我希望它可以说我也可以说company.industry.name没有问题。但是,我收到了一个无用的错误:

wrong constant name Refinery:Industries

我最终想做的是一直跟随我的协会,即work.company.industry.name,但看起来公司和行业之间的链断裂了。 我在这做错了什么?这是我的代码更详细。


代码

这是我的模特。任何想法会阻止我从关联公司获取行业属性,因为行业有多家公司(公司lol)和公司属于一个行业?任何帮助将非常感激。

行业模式

module Refinery
  module Industries
    class Industry < Refinery::Core::BaseModel
      ...

      attr_accessible :name, :description, :position
      has_many :companys, :class_name => '::Refinery::Companys::Company', :dependent => :nullify
      has_many :works, :through => :companys
    end
  end
end

公司模式

module Refinery
  module Companys
    class Company < Refinery::Core::BaseModel
      ...

      attr_accessible :name, :position, :industry_id
      has_many :works, :class_name => '::Refinery::Works::Work', :dependent => :destroy
      belongs_to :industry, :class_name => '::Refinery:Industries::Industry'
    end
  end
end

工作模式

module Refinery
  module Works
    class Work < Refinery::Core::BaseModel
      ...

      attr_accessible :name, :description, :position, :company_id
      belongs_to :thumbnail, :class_name => '::Refinery::Image'
      belongs_to :Company, :class_name => '::Refinery::companys::company'
    end
  end
end

然后在我的erb文件中我这样做:

<% @works.each do |work| %>          
    ...
    <h5>
      <%= work.company.name %>
    </h5>
<% end %>    

那个有用。
这个给了我一个错误:

 <% @clients.each do |client| %>
   <h5>
       <%= client.industry.name %>
   </h5>
 <% end %>

该错误如下:

wrong constant name Refinery:Industries

1 个答案:

答案 0 :(得分:1)

Company模型中至少缺少双重结肠:

belongs_to :industry, :class_name => '::Refinery:Industries::Industry'

应该是

belongs_to :industry, :class_name => '::Refinery::Industries::Industry'

我还没有真正查看其余的代码,但这是第一个错误。