我正在努力建立has_many:通过卖方和买方之间的关系来产品。
product.rb
class Product < ActiveRecord::Base
resourcify
include Authority::Abilities
self.authorizer_name = 'ProductAuthorizer'
belongs_to :seller, :class_name => 'User', :foreign_key => 'seller_id', required: false
has_many :purchasements
has_many :buyers, :class_name => 'User', through: :purchasements
has_many :comments, as: :commentable, dependent: :destroy
end
purchasement.rb
class Purchasement < ApplicationRecord
belongs_to :buyer
belongs_to :purchase
end
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
rolify
include Authority::UserAbilities
has_many :sales, :class_name => 'Product', :foreign_key => 'sale_id'
has_many :purchasements
has_many :purchases, :class_name => 'Product', through: :purchasements
end
产品迁移
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :title
t.text :content
t.integer :price
t.belongs_to :seller, index: true
t.integer :purchase_count, default: 0, null: false
t.timestamps null: false
end
end
end
购买迁移
class CreatePurchasements < ActiveRecord::Migration[5.0]
def change
create_table :purchasements do |t|
t.belongs_to :buyer, index: true #상품 입장에서의 구매자들
t.belongs_to :purchase, index: true #구매자 입장에서의 상품들
t.timestamps
end
end
end
products_controller.rb
def create
@product = Product.new(product_params)
@product.seller = current_user
authorize_action_for @product
#remote true 가 적용된 폼에는 token 이 포함되지 않는다. 따로 포함하라고 말해줘야함.
if @product.save
redirect_to @product, notice: '상품 업로드 완료'
else
Rails.logger.info(@product.errors.inspect)
flash[:error] = 'fjeislafsa'
render :new
end
end
def download
if points_check(@product)
@product.purchase_count += 1
@product.buyers << current_user
#current_user.purchases << @product
@product.save
redirect_to @product.file.expiring_url(10)
else
redirect_to payments_path
end
end
问题是当用户尝试下载(购买)产品时。
@ product.buyers不起作用并给出此错误'NameError:uninitialized constant Purchasement :: Buyer “
我的目标是让产品只有一个卖家(用户),并拥有多个买家(用户)。另一方面,用户可以同时拥有许多销售(产品)和购买(产品)。
我在某处感到困惑,无法找到修复的地方。
非常感谢。
额外错误
Started GET "/products/download/1" for 127.0.0.1 at 2017-03-26 18:22:10 +0900
Processing by Shop::ProductsController#download as HTML
Parameters: {"id"=>"1"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (0.2ms) UPDATE "users" SET "point" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["point", 9948], ["updated_at", 2017-03-26 09:22:10 UTC], ["id", 1]]
(1.1ms) COMMIT
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 64ms (ActiveRecord: 8.9ms)
ActiveModel::UnknownAttributeError (unknown attribute 'product_id' for Purchasement.):
app/controllers/shop/products_controller.rb:67:in `download'
Rendering /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.9ms)
Rendering /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.9ms)
Rendering /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /Users/mac/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (53.5ms)
答案 0 :(得分:0)
class Purchasement < ApplicationRecord
belongs_to :buyer
belongs_to :purchase
end
鉴于上述声明,Rails尝试将买方协会与买方类关联,并与Purchase类购买关联,两者都不存在。
您还应在此处指定模型类名称:
class Purchasement < ApplicationRecord
belongs_to :buyer, class_name: 'User'
belongs_to :purchase, class_name: 'Product'
end