我正在嘗試使用rails 5的多態關係。Rails多態關係
我很難弄清楚如何完成我的關係。
我的用戶,誰可以採取的賓館,飯店預訂等
我的目的是通過API調用/用戶/(編號)時得到的酒店名稱和保留。
這裏是我的用戶模型:
class User < ApplicationRecord
has_many :reservations, as: :reservable
has_many :hotels, through: :reservations
end
我的酒店模式:
class Hotel < ApplicationRecord
has_many :reservations, as: :reservable
belongs_to :users, through: :reservations
end
的訂房模式:
class Reservation < ApplicationRecord
belongs_to :reservable, polymorphic: true
belongs_to :users
belongs_to :hotels
end
遷移:
用戶:
個class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.timestamps
end
end
end
預訂:
class CreateReservations < ActiveRecord::Migration[5.0]
def change
create_table :reservations do |t|
t.belongs_to :hotel, index: true
t.belongs_to :user, index: true
t.datetime :date_from
t.datetime :date_to
t.timestamps
end
end
end
class ReservationPolymorphism < ActiveRecord::Migration[5.0]
def change
rename_column :reservations, :hotel_id, :reservable_id
change_column :reservations, :reservable_id, polymorphic: true
add_column :reservations, :reservable_type, :string
end
end
酒店:
class CreateHotels < ActiveRecord::Migration[5.0]
def change
create_table :hotels do |t|
t.string :name
t.string :address
t.string :postal_code
t.string :town
t.timestamps
end
end
end
我只是在我的預訂表1線:
mysql> select * from reservations;
+----+---------------+-----------------+---------+---------------------+---------------------+---------------------+---------------------+
| id | reservable_id | reservable_type | user_id | date_from | date_to | created_at | updated_at |
+----+---------------+-----------------+---------+---------------------+---------------------+---------------------+---------------------+
| 1 | 1 | Hotel | 1 | 2017-01-12 00:00:00 | 2017-01-15 00:00:00 | 2016-10-19 09:18:01 | 2016-10-19 09:18:01 |
+----+---------------+-----------------+---------+---------------------+---------------------+---------------------+---------------------+
我使用API的時候沒有結果。
這裏是我所得到的,使用Rails控制檯:
2.2.3 :001 > thomas = User.find(1)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
=> #<User id: 1, first_name: "Thomas", last_name: "Dupont", email: "[email protected]", created_at: "2016-10-18 21:12:12", updated_at: "2016-10-18 21:12:12">
2.2.3 :003 > thomas.reservations
Reservation Load (0.3ms) SELECT `reservations`.* FROM `reservations` WHERE `reservations`.`reservable_id` = 1 AND `reservations`.`reservable_type` = 'User'
2.2.3 :005 > thomas.hotels
NameError: uninitialized constant User::Hotels
所以我可以看到我做基本的錯誤與軌道的關係和多態,但我真的無法找出我錯了。
我想我犯了一個錯誤。 我認爲多態性可能是用來加載其他模型和它們的表(如「morphTo」以雄辯/ laravel),而它只是載入沒有數據表示(在這篇文章中描述:https://robots.thoughtbot.com/using-polymorphism-to-make-a-better-activity-feed-in-rails)模型
您能刪除用戶和酒店模型中寫入的額外關聯嗎?請從酒店刪除以下'has_many:hotels,通過::reservations'和'belongs_to:users,通過::reservations'。然後在控制檯中運行查詢 –
只是做了它,仍然得到了「預訂加載(0.4ms)SELECT'預訂'。*從'預訂'預訂'WHERE'預訂'.'''reservable_id' = 1和'reservations'.'reservable_type' ='User' =># 「的問題。 –
galettan
可否請您從預訂模式中刪除此項,belongs_to:用戶 belongs_to:酒店 –