以下是我的模特:
class Pizza < ActiveRecord::Base
belongs_to :pizza_type
has_many :toppings, through: :pizza_toppings
end
class Topping < ActiveRecord::Base
belongs_to :topping_type
has_many :pizza_size_topping_prices
has_many :pizzas, through: :pizza_toppings
end
class PizzaTopping < ActiveRecord::Base
belongs_to :pizza
belongs_to :topping
end
数据库架构
ActiveRecord::Schema.define(version: 20140108194303) do
create_table "pizza_size_topping_prices", force: true do |t|
t.integer "pizza_size_id"
t.integer "topping_id"
t.float "half_price"
t.float "whole_price"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pizza_sizes", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pizza_toppings", force: true do |t|
t.integer "pizza_id"
t.integer "topping_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pizza_type_size_prices", force: true do |t|
t.integer "pizza_size_id"
t.integer "pizza_type_id"
t.float "base_price"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pizza_types", force: true do |t|
t.string "name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pizzas", force: true do |t|
t.integer "pizza_type_id"
t.string "name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "topping_types", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "toppings", force: true do |t|
t.integer "topping_type_id"
t.string "name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
end
当我尝试通过white_pizza.toppings找到比萨饼的浇头时,我收到错误:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :pizza_toppings in model Pizza
以下是IRB控制台:
Loading development environment (Rails 4.0.0)
irb(main):001:0> white_pizza = Pizza.last
Pizza Load (1.1ms) SELECT "pizzas".* FROM "pizzas" ORDER BY "pizzas"."id" DESC LIMIT 1
=> #<Pizza id: 3, pizza_type_id: 5, name: "first white pizza", description: "first white pizza description", created_at: "2014-01-08 20:20:25", updated_at: "2014-01-08 20:20:25">
irb(main):002:0> white_pizza.toppings
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :pizza_toppings in model Pizza
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/reflection.rb:550:in `check_validity!'
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:25:in `initialize'
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_through_association.rb:9:in `initialize'
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `new'
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations.rb:157:in `association'
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/builder/association.rb:70:in `toppings'
from (irb):2
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/gcardella/.rvm/gems/ruby-2.0.0-p0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
任何人都知道为什么会这样吗?我认为基于这种关系,我可以通过这种方式获得披萨的许多配料。请指教。
答案 0 :(得分:0)
您的Pizza
和Topping
类都需要以下行,否则has_many :through
部分将无法正常运行:
has_many :pizza_toppings