0

私は、デフォルトとユーザー定義の2種類の製品があるところにアプリケーションを作っています。 DefaultProductはこのフィールドを必要としませんが、ユーザーとUserProductsだけを関連付けることを試みています。私はしばらくオンラインで見てきましたが、決定的なものは見つかりませんでした。ここでrails - 継承クラスは別のモデルに属します

は、私がこれまで試した試みだ:私はSTIを使用して疑う

class Product < ActiveRecord::Base 
    validates :name, precence: true, length: { maximum: 100 } 

    has_many :categories 
end 

class DefaultProduct < Product 
    def self.model_name 
    Product.model_name 
    end 
end 

class UserProduct < Product 
    def self.model_name 
    Product.model_name 
    end 

    belongs_to :user # Causes the console to spew errors 
end 

は問題の大きな要因であるが、レールにおよび代替の知らない新しいですよ。

他のモデルと継承モデルをレールに関連付ける一般的な方法は何ですか?

答えて

2

すべての子モデルは親テーブルにとどまっているため、productsテーブルにuser_idフィールドを生成する必要があります。また、type:stringフィールドをレール目的で追加する必要があります。

+0

これはうまくいきました。ありがとうございました! –

関連する問題