model所形成的關聯
取得目前專案所用到的資源
/Gemifile
gem 'rails', '4.2.0'
gem 'hirb-unicode'
更新指令
$ bundle update
一對多(關聯)
01.建立一個model,命名為Product
$ rails g model Product
/db/migrate (product)
t.string :title
t.text :description
t.integer :price
$ rake db:migrate
建立一個model,命名為Store
$ rails g model Store
/db/migrate (store)
t.string :name
$ rake db:migrate
- 在product增加一個新欄位store_id
$ rails g migration product_store_id
/db/migrate (product_store_id)
add_column :products, :store_id, :integer
- 在product減少一個欄位store_id
$ rails g migration delete_store_id
/db/migrate (delete_store_id)
remove_column(:products, :store_id)
$ rake db:migrate
/app/models/product.rb
belongs_to :store
/app/models/store.rb
has_many :products
02.進入rails後台
$ rails c
- 查複數型態
"student".pluralize
=>"students"
- 啟用Hirb功能,排列較為整齊
Hirb.enable
=>true
- 新增product的資料
Product.create(title:'ruby book', price:100)
=>#<Product id: 1, title: "ruby book", description: nil, price: 100>
Product.all
=>+----+------------+-------------+-------+----------+
| id | title | description | price | store_id |
+----+------------+-------------+-------+----------+
| 1 | ruby book | | 100 | |
| 2 | rails book | | 150 | |
| 3 | php book | | 10 | |
+----+------------+-------------+-------+----------+
Store.create(name:'Ruby store')
=> #<Store id: 1, name: "Ruby store">
Store.all
=>+----+------------+
| id | name |
+----+------------+
| 1 | ruby store |
| 2 | PHP store |
+----+------------+
- 設定p1,p2,s1
p1=Product.first
s1=Store.first
p1.store=s1
- 單看p1可以看到剛剛設的store_id,但因未存檔在在all裡看不到
p1
=>+----+-----------+-------------+-------+----------+
| id | title | description | price | store_id |
+----+-----------+-------------+-------+----------+
| 1 | ruby book | | 100 | 1 |
+----+-----------+-------------+-------+----------+
Product.all
=>+----+------------+-------------+-------+----------+
| id | title | description | price | store_id |
+----+------------+-------------+-------+----------+
| 1 | ruby book | | 100 | |
| 2 | rails book | | 150 | |
| 3 | php book | | 10 | |
+----+------------+-------------+-------+----------+
- 進行存檔,就可以在all裡看到
p1.save
Product.all
=>+----+------------+-------------+-------+----------+
| id | title | description | price | store_id |
+----+------------+-------------+-------+----------+
| 1 | ruby book | | 100 | 1 |
| 2 | rails book | | 150 | |
| 3 | php book | | 10 | |
+----+------------+-------------+-------+----------+
p2=Product.find(2)
p2.store=s1
p2.save
=>+----+------------+-------------+-------+----------+
| id | title | description | price | store_id |
+----+------------+-------------+-------+----------+
| 1 | ruby book | | 100 | 1 |
| 2 | rails book | | 150 | 1 |
| 3 | php book | | 10 | |
+----+------------+-------------+-------+----------+
- 查詢結果
p1.store #第一樣商品在第一間商店販賣
=>+----+-------------+
| id | name |
+----+-------------+
| 1 | ruby store |
+----+-------------+
s1.products #第一間商店賣第一和第二樣商品
=>+----+------------+-------------+-------+----------+
| id | title | description | price | store_id |
+----+------------+-------------+-------+----------+
| 1 | ruby book | | 100 | 1 |
| 2 | rails book | | 150 | 1 |
+----+------------+-------------+-------+----------+
多對多(關聯)
01.建立新的model,名為Warehouse
$ rails g model Warehouse
db/migrate (warehouse)
t.belongs_to :product
t.belongs_to :store
app/models/warehouse.rb
belongs_to :product
belongs_to :store
app/models/product.rb
has_many :warehouses
has_many :stores, :through => :warehouses
app/models/store.rb
has_many :warehouses
has_many :products, :through => :warehouses
02.進入rails後台
$ rails c
- 建立資料
Product.create(title:'ruby', price:250)
Product.create(title:'rails', price:200)
Product.create(title:'PHP', price:150)
Store.create(name:'ruby shop')
Store.create(name:'PHP store')
- 指定給p1,p2,p3,s1,s2
p1=Product.find(1)
p2=Product.find(2)
p3=Product.find(3)
s1=Store.find(1)
s2.Store.find(2)
- 設定各商店的商品
s1.products=[p1, p2]
s2.products=[p1, p2, p3]
- 結果查詢
s1.products #在第一間商店賣第一和第二樣商品
=>+----+-------+-------------+-------+
| id | title | description | price |
+----+-------+-------------+-------+
| 1 | ruby | | 250 |
| 2 | rails | | 200 |
+----+-------+-------------+-------+
p1.stores #第一樣商品在第一和第二間商店販賣
=>+----+-----------+
| id | name |
+----+-----------+
| 1 | ruby shop |
| 2 | PHP store |
+----+-----------+
沒有留言:
張貼留言