我是编码方面的新手,我正在发现RoR。 我想创建一个包含用户和产品的应用程序。我擅长与用户,但我想了解一下该产品的架构......如何使用Ruby on Rails建立嵌套模型?
这里的产品型号我想象:
Product :
- Name
- Owner
- Borrower
- Location
- Address
- GPS Coordinates
- Comments
- Title
- Content
- User
- Category
这里是我的主要型号有:
Class User
has_many :owned_products, class_name: "Product", foreign_key: "owner_id"
has_many :borrowed_products, class_name: "Product", foreign_key: "borrower_id"
has_one :location, as: :localizable
end
$ rails generate model Location address:string coordinates:string
Class Location
belongs_to :localizable, polymorphic: true
end
$ rails generate model Comment title:string content:text user_id:integer
Class Comments
belongs_to :product
has_one :user
end
$ rails generate model Product name:string owner_id:integer borrower_id:integer location_id:integer comment_id:integer category_id:integer
Class Product
belongs_to :owner, class_name: "User", foreign_key: "owner_id"
belongs_to :borrower, class_name: "User", foreign_key: "borrower_id"
has_one :location, as: :localizable
has_many :comments
has_one :category
end
所以,我想知道如果我要创建每个类别的子模型是这样的:
- Consumption
- Name
- Utility
- Builder
- Culture
- Title
- Barcode
- Type
- Book
- Author
- Publisher
- Video
- Length
- Format
- Food
- Name
- Cooking Date
- Weight
其中我会写下这样的:
$ rails generate model Category consumption_id:integer culture_id:integer food_id:integer
Class Category
belongs_to :product
has_one :consumption
has_one :culture
has_one :food
end
$ rails generate model Consumption name:string utility:string builder:string
Class Consumption
belongs_to :category
end
$ rails generate model Culture title:string barcode:integer type_id:integer
Class Culture
belongs_to :category
has_one :type
end
$ rails generate model Type book_id:integer video_id:integer
Class Type
belongs_to :culture
has_one :book
has_one :video
end
$ rails generate model Book author:string publisher:string
Class Book
belongs_to :type
end
$ rails generate model Video length:string format:string
Class Video
belongs_to :type
end
$ rails generate model Food name:string cooking_date:datetime weight:float
Class Food
belongs_to :category
end
或者,如果这将是更好地保持在这样的只有一个级别:
- Category_type (consumption, culture, or food)
- Name (or title)
- Utility
- Builder
- Barcode
- Culture_type
- Author
- Publisher
- Length
- Format
- Cooking_date
- Weight
它会给我:
$ rails generate model Category category_type:string name:string utility:string builder:string barcode:integer culture_type:string author:string publisher:string length:integer format:string cooking_date:datetime weight:float
Class Category
belongs_to :product
end
我觉得第一种方式更容易理解,但我怀疑它会如第二种那样高效。你能给我一个提示吗?
因此改为@ R4M,这里是我做过什么:
$ rails generate model Category
Class Category
belongs_to :product
belongs_to :categorizable, polymorphic: true
end
$ rails generate model Consumption name:string utility:string builder:string
Class Consumption
has_many :categories, :as => :categorizable
end
$ rails generate model Culture title:string barcode:integer type_id:integer
Class Culture
has_many :categories, :as => :categorizable
has_one :type
end
$ rails generate model Food name:string cooking_date:datetime weight:float
Class Food
has_many :categories, :as => :categorizable
end
我改变了分类的迁移:
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.references :categorizable, polymorphic: true
t.timestamps
end
end
end
但它似乎并不十分理解我...
如果我想抓住@user的每个“消费”产品,我该怎么办?做这样的事情可以工作吗?
@consumptions = @user.products.consumptions
感谢您的回答! 您是否意味着我必须改变我的类别类: 类分类<的ActiveRecord :: Base的 belongs_to的:categorizable,多态=>真正 结束 类文化/消费/食品<的ActiveRecord :: Base的 HAS_ONE :category,:as =>:分类 结束 – 2013-05-13 15:08:57
是的男人。请记住,您必须相应地修改数据库,也就是创建相应的迁移:您需要在声明多态接口的模型中声明外键列和类型列。 – r4m 2013-05-13 15:18:05
我觉得我失去了一些东西... 我的位置多态模型做得好吗?我是否应该修改迁移? – 2013-05-13 15:38:18