私は現在、アプリケーションにCategory
モデルを実装しようとしています。私はUsers
が多くの場合Categories
を持つことができるように設計しようとしており、Groups
も可能です。Ruby on Rails - カテゴリとの多相関連
私が遭遇している問題は、User
またはGroup
に割り当てられていないCategories
の正規リストを持つことができるようにしたいということです。
私はrubyonrails.org/association_basicsを参照していました。
class CreateCategories < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
t.string :name
t.text :description
t.references :categorizable, polymorphic: true, index: true
t.timestamps
end
end
end
class Category < ApplicationRecord
belongs_to :categorizable, :polymorphic => true
end
class User < ApplicationRecord
has_many :categories, :as => :categorizable
end
class Group< ApplicationRecord
has_many :categories, :as => :categorizable
end
私はrails c
を通じて新しいCategory
を作成しようとしているが、私は保存しようとするたびに、私はいくつかの条件が欠けているので、それはおそらく私のトランザクションをロールバックします。
Category(id: integer, name: string, description: text, created_at: datetime, updated_at: datetime)
Category.create(:id => 1, :name => 'Category_1', :description => '')
begin transaction
rollback transaction
新しいCategory
を作成するためのより良い方法があるように私は手動でid
を設定すべきではないと私も、感じます。
ありがとうございました。
レール5を使用していますか? – usha
IDを設定してはならない場合、なぜそれをやっていますか? 'begin transaction'と' rollback transaction'はログにすべて表示されますか? – chumakoff