2017-03-26 49 views
0

我正在嘗試建立has_many:通過賣方和買方的產品關係。Rails,在賣方和買方內部有很多貫穿的關係

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:未初始化的常量::統購統銷買家 」

我的目標是使產品只有一個賣方(用戶),並有多個買方(用戶)。另一方面,用戶可以同時擁有許多銷售(產品)和購買(產品)。

我在某處弄糊塗了,找不到要修復的地方。

提前感謝。

額外的錯誤

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
class Purchasement < ApplicationRecord 
    belongs_to :buyer 
    belongs_to :purchase 
end 

鑑於上述聲明,Rails的嘗試買家協會與買方類和採購協會與採購類,兩者不存在關聯。

你應該在這裏指定的模型類的名字,以及:

class Purchasement < ApplicationRecord 
    belongs_to :buyer, class_name: 'User' 
    belongs_to :purchase, class_name: 'Product' 
end 
+0

如何有關Purchasements的遷移文件?我是否也應該改變它? –

+0

購買的未知屬性'product_id'。我現在得到這個錯誤... –

+0

除非你想添加一個外鍵(在這種情況下,你將不得不指定'foreign_key:{to_table :: users}'),遷移不需要改變。 – lorefnon