2017-03-26 7 views
0

私はhas_manyを構築しようとしています。製品レール、売り手と買い手の中で多くの関係を持っています

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 
ため

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 

マイグレーションpurchasement

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:初期化されていない定数Purchasement ::バイヤー 」を与えていない

私の目標は、製品を作ることであるユーザー(唯一の売り手(ユーザー)を持っている、と複数の買い手を持っています)。反対に、ユーザーは多数の販売(製品)と購入(製品)を同時に持つことができます。

私はどこか混乱して、修正する場所を見つけることができません。

ありがとうございます。上記の宣言を考えると

余分なエラー

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 

は、存在しない、どちらも購入クラスと買い手のクラスおよび購入関連して、買い手の関連付けを関連付ける試みを、レール。

あなたはここにもモデルのクラス名を指定する必要があります。Purchasementsの移行ファイルは約

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

どのように?私もこれを変更する必要がありますか? –

+0

不明な属性 'product_id'を購入しました。私は今このエラーを受け取ります... –

+0

外部キーを追加したい場合( 'foreign_key:{to_table::users}'を指定する必要があります)、移行を変更する必要はありません。 – lorefnon

関連する問題