2017-08-17 94 views
0

我在我的程序中有3個主要模型;用戶,國家,城市。在Rails中創建模型關係的問題

  • 用戶和鄉村有許多一對多的關係,我與 跳閘模式加入。

  • 用戶和城市有多對多的關係,我加入了 訪問模式。

  • 國家和城市有一對多的關係。

當我運行rails db:migrate我沒有得到任何錯誤,當我嘗試和種子數據或進入控制檯創建一個城市也不會保存所有出現很好,但是。任何用戶或國家都將成功創建,並且我可以在它們之間建立關係。

查看我的模特兒。

user.rb

class User < ApplicationRecord 

    before_save { self.email = email.downcase } 
    #attr_accessible :user_name, :email 
    validates_confirmation_of :password 
    has_secure_password 

    validates :user_name, presence: true, length: { maximum: 25 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i 
    validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX } 
    validates :password, presence: true, confirmation: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

    has_many :trips 
    has_many :visits 
    has_many :countries, through: :trips 
    has_many :cities, through: :visits 


end 

city.rb

class City < ApplicationRecord 

    has_many :visits 
    has_many :users, through: :visits 
    belongs_to :country 

end 

country.rb

class Country < ApplicationRecord 

    has_many :trips 
    has_many :cities 
    has_many :users, through: :trips 

end 

trip.rb

class Trip < ApplicationRecord 

    belongs_to :country 
    belongs_to :user 

end 

visit.rb

class Visit < ApplicationRecord 

    belongs_to :city 
    belongs_to :user 

end 

我本來甚至沒有訪問模型,我只是過跳閘模型都加入許多一對多的關係。但是,在試圖解決這個問題時,我將它分開了。

任何有關這個問題的幫助,將不勝感激。如果您需要更多信息,請告訴我。

+0

你確定你有所有模型的遷移? db:如果無事可做,遷移將成功返回。當你試圖保存時,你會得到什麼樣的信息? (嘗試使用保存!而不是從控制檯保存,如果它只是返回false)。 – cpm

+0

我得到'ActiveRecord :: RecordInvalid:驗證失敗:國家必須存在',當一個保存!我有國家,我嘗試了一些像'Country.find(1).cities << [City.create(name:「Toronto」)]'',希望在創建該城市時將該城市分配給該國家將有所幫助,但不會運氣。 –

+0

你用country_id保存城市嗎? – hashrocket

回答

0

通過參觀城市的has_many用戶,那麼你必須聲明的has_many:城市模型內部訪問下面的示例代碼也許可以幫助你的問題

class City < ApplicationRecord 
    has_many :visits 
    has_many :users, through: :visits 
    belongs_to :country 
end 

當您在控制檯中創建的城市,要確保你給COUNTRY_ID在belongs_to的國家,這裏是一個示例代碼:

@city = City.new 
@city.country = Country.first 
# @city.name = .... # your seed code 
@city.save 
+0

謝謝,我已經添加,以及到用戶模型,但我仍然有同樣的問題。 我編輯了上面的代碼以添加更改。 –

+0

好吧,我剛剛編輯了我的答案 – widjajayd

1

我會通過適當的建模也開始:

class City < ApplicationRecord 
    has_many :visits 
    has_many :users, through: :visits 
    belongs_to :country 
end 

class Country < ApplicationRecord 
    has_many :trips 
    has_many :cities 
    has_many :users, through: :trips 
end 

class Trip < ApplicationRecord 
    belongs_to :country 
    belongs_to :user 
    has_many :visits 
    has_many :cities, through: :visits 
end 

class Visit < ApplicationRecord 
    belongs_to :trip 
    belongs_to :city 
    has_one :country, through: :city 
    has_one :user, through: :trip 
end 

# added 
class User < ApplicationRecord 
    # ... 
    has_many :trips 
    has_many :visits, through: :trips 
    has_many :countries, through: :trips 
    has_many :cities, through: :visits 
end 

這會創建Trip和Visit之間的一對多關聯,並避免在兩者上覆制user_id外鍵。

在Rails 5中,主要變化之一是默認情況下belongs_to關聯是非可選的。

因此,如果您嘗試創建沒有國家的城市,驗證將失敗。但是,如果從現有的記錄創建城市:

Country.first.cities.create!(name: 'Toronto') 

,或通過記錄:

City.create!(name: 'Toronto', country: Country.first) 

驗證將通過。

您還可以設置關聯作爲可選這是在軌道4,5的行爲:

class City < ApplicationRecord 
    has_many :visits 
    has_many :users, through: :visits 
    belongs_to :country, optional: true 
end 
+0

但是我做了你所建議的改變,但是當我嘗試創建一個城市時,它仍然不起作用。我得到錯誤 - 'ActiveModel :: UnknownAttributeError:城市'的未知屬性'country_id'。我的用戶模型是否正確設置? –

+0

'ActiveModel :: UnknownAttributeError'意味着你還沒有創建'country_id'列。如果您創建了這樣的遷移,您可能會忘記運行它。 – max

+0

否則,您可以通過運行'rails g migration add_country_id_to_cities country_id:belongs_to'生成遷移。 – max