Ruby - 关系问题 (has_one/has_many)
Ruby - Relationship issues (has_one/has_many)
我正在尝试创建一种方法来管理 Items
个 User
或 Event
的数量。我以为我可以通过另一个模型来做到这一点,ItemDatum
。
我在想 ItemDatum
属于 User
或 Event
,以及 has_many
Items
。
class ItemDatum < ActiveRecord::Base
belongs_to :event
belongs_to :user
has_many :items
end
Event
会有一个 ItemDatum
并且有很多 Items
到 ItemDatum
class Event < ActiveRecord::Base
...
has_one :item_datum, dependent: :destroy
has_many :items, through: :item_datum
end
并且 Items
将属于许多 ItemDatum
class Item < ActiveRecord::Base
has_and_belongs_to_many :item_datum
...
end
然而,这不是很有效,我没有正确调试它的知识。
如果也有帮助的话,这里是迁移:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :name
t.string :text
t.integer :location_id
t.integer :item_datum_id
t.timestamps null: false
end
end
end
class CreateItemData < ActiveRecord::Migration
def change
create_table :item_data do |t|
t.integer :item_id
t.integer :event_id
t.integer :user_id
t.timestamps null: false
end
end
end
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :name
t.string :description
t.string :item_type
t.integer :value
t.integer :item_datum_id
t.timestamps null: false
end
add_index :items, :name, unique: true
end
end
如果您希望一个项目有多个项目基准,那么您就缺少 table。我通常更喜欢指定模型而不是使用 habtm。
rails g model ItemDataItem item_data_id:integer item_id:integer
class ItemDataItem < ActiveRecord::Base
belongs_to :item_data
belongs_to :item
end
class Item < ActiveRecord::Base
has_many :item_data_items
has_many :item_datum, through: :item_data_items
end
然后在ItemDataclass上,添加相同的has_many和has_many通过。我的一些名字可能有复数错误,但它应该可以解决问题。
编辑
如果物品只属于一个数据,只需更新物品模型:
class Item < ActiveRecord::Base
belongs_to :item_datum
...
end
我正在尝试创建一种方法来管理 Items
个 User
或 Event
的数量。我以为我可以通过另一个模型来做到这一点,ItemDatum
。
我在想 ItemDatum
属于 User
或 Event
,以及 has_many
Items
。
class ItemDatum < ActiveRecord::Base
belongs_to :event
belongs_to :user
has_many :items
end
Event
会有一个 ItemDatum
并且有很多 Items
到 ItemDatum
class Event < ActiveRecord::Base
...
has_one :item_datum, dependent: :destroy
has_many :items, through: :item_datum
end
并且 Items
将属于许多 ItemDatum
class Item < ActiveRecord::Base
has_and_belongs_to_many :item_datum
...
end
然而,这不是很有效,我没有正确调试它的知识。
如果也有帮助的话,这里是迁移:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :name
t.string :text
t.integer :location_id
t.integer :item_datum_id
t.timestamps null: false
end
end
end
class CreateItemData < ActiveRecord::Migration
def change
create_table :item_data do |t|
t.integer :item_id
t.integer :event_id
t.integer :user_id
t.timestamps null: false
end
end
end
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :name
t.string :description
t.string :item_type
t.integer :value
t.integer :item_datum_id
t.timestamps null: false
end
add_index :items, :name, unique: true
end
end
如果您希望一个项目有多个项目基准,那么您就缺少 table。我通常更喜欢指定模型而不是使用 habtm。
rails g model ItemDataItem item_data_id:integer item_id:integer
class ItemDataItem < ActiveRecord::Base
belongs_to :item_data
belongs_to :item
end
class Item < ActiveRecord::Base
has_many :item_data_items
has_many :item_datum, through: :item_data_items
end
然后在ItemDataclass上,添加相同的has_many和has_many通过。我的一些名字可能有复数错误,但它应该可以解决问题。
编辑
如果物品只属于一个数据,只需更新物品模型:
class Item < ActiveRecord::Base
belongs_to :item_datum
...
end