我是编码的新手,我发现了RoR。 我想用Users和Products创建一个应用程序。我很擅长用户,但我想知道产品的架构......
这里是我想象的产品型号:
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