2016-10-19 79 views
0

我正在嘗試使用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)模型

+0

您能刪除用戶和酒店模型中寫入的額外關聯嗎?請從酒店刪除以下'has_many:hotels,通過::reservations'和'belongs_to:users,通過::reservations'。然後在控制檯中運行查詢 –

+0

只是做了它,仍然得到了「預訂加載(0.4ms)SELECT'預訂'。*從'預訂'預訂'WHERE'預訂'.'''reservable_id' = 1和'reservations'.'reservable_type' ='User' =># 「的問題。 – galettan

+0

可否請您從預訂模式中刪除此項,belongs_to:用戶 belongs_to:酒店 –

回答

0

我想在查看你的代碼時,你已經在多態關聯之上寫了額外的關聯,爲了創建一個多態關聯,只需要下面的代碼就足夠了。無需在模型內傳遞額外關聯。請重構代碼如下, 用戶模型

class User < ApplicationRecord 
    has_many :reservations, as: :reservable 
end 

酒店型號:

class Hotel < ApplicationRecord 
     has_many :reservations, as: :reservable 
end 

預訂模型:

class Reservation < ApplicationRecord 
     belongs_to :reservable, polymorphic: true 
end 

而且在保留遷移文件,添加預留如下

t.references :reservable, polymorphic: true 
+0

好吧,我得到了我犯的第一個錯誤:用戶不可預訂。用戶has_many通過user_id關係保留,而不是「reservable_id」。 我必須弄清楚用戶 - >酒店關係,以使其充分發揮作用。 – galettan

+0

它一直在尋找「2.2.3:001> u = User.find(1) 用戶負載(0.5ms)SELECT'users'。* FROM'users' WHERE' users'.'id' = 1 LIMIT 1 = >#<用戶ID:1,名字:「Thomas」,姓氏:「杜邦」,電子郵件:「[email protected]」,created_at:「2016-10-18 21:12:12」,updated_at:「 2016-10-18 21:12:12「> 2.2.3:002>保留 保留加載(0.4ms)SELECT'reservations'。* FROM'reservations' WHERE' reservations'.'reservable_id' = 1 AND 'reservations'.'reservable_type' ='User' =>#「。這不是我正在尋找的行爲。 – galettan

0

好吧,我整理了這一點:

這是訂房模式:

class Reservation < ApplicationRecord 
     belongs_to :reservable, polymorphic: true 
end 

這是酒店型號:

class Hotel < ApplicationRecord 
end 

而且用戶模式:

class User < ApplicationRecord 
    has_many :reservations 
end 

以及我如何在滑軌控制檯中獲得成果:

Running via Spring preloader in process 87452 
Loading development environment (Rails 5.0.0.1) 
2.2.3 :001 > u = User.find(1) 
    User Load (0.3ms) 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 :002 > u.reservations 
    Reservation Load (0.3ms) SELECT `reservations`.* FROM `reservations` WHERE `reservations`.`user_id` = 1 
=> #<ActiveRecord::Associations::CollectionProxy [#<Reservation id: 1, reservable_id: 1, reservable_type: "Hotel", user_id: 1, date_from: "2017-01-12 00:00:00", date_to: "2017-01-15 00:00:00", created_at: "2016-10-19 09:18:01", updated_at: "2016-10-19 09:18:01">]> 
2.2.3 :003 > u.reservations.first.reservable 
    Hotel Load (0.3ms) SELECT `hotels`.* FROM `hotels` WHERE `hotels`.`id` = 1 LIMIT 1 
=> #<Hotel id: 1, name: "HYATT REGENCY HOTEL", address: "3 Place du Général Kœnig", postal_code: "75017", town: "PARIS", created_at: "2016-10-19 08:36:55", updated_at: "2016-10-19 08:36:55">