开发者

Rails nested resources (many levels) Class treated as Module

These are my models:

class Company < ActiveRecord::Base  
  has_many :products  
end

class Product < ActiveRecord::Base  
  belongs_to :company  
  has_many :prices  
end

class Price < ActiveRecord::Base  
  belongs_to :product  
end

I defined them in routes as nested resources

resources :companies  
namespace :company do  
  scope ":company_id" do  
    resources :products do  
      resources :prices  
      resources :production_capabilities  
    end  
  end  
end

I wanted to put controllers and views in catalogs matching that structure

app/controllers/companies_controller.rb  
app/controllers/company/products_controller.rb  
app/controllers/company/product  
app/controllers/company/product/prices_controller.rb

As soon as i create product directory inside company and i try to call

Company.find(1).products

i get

NoMethodError: undefined method 'quoted_table_name' for Company::Product:Module

Does anybody know wh开发者_开发知识库at am i doing wrong?


The Rails documentation explicitly recommends that we don't nest resources more than 1 level deep:

http://guides.rubyonrails.org/routing.html#nested-resources

You'll get URLs like this:

/company/1/product/4/price/5

That's not pretty. Try to avoid it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜