2014-10-04 58 views
0
1. country=Country.create(:name=>'Australia') 
2. country.states 
    #<ActiveRecord::Associations::CollectionProxy []> 

3. first_state=State.new(:name=>'Queensland') 
=> #<State id: nil, country_id: nil, name: "Queensland", created_at: nil, update 
d_at: nil> 
4. first_state.country 
=> nil 

5.country.states=first_state 

**NoMethodError: undefined method `each' for #<State:0x438a110>** 

在型號:NoMethodError:未定義的方法`每個」爲#<國家:0x438a110>

class State < ActiveRecord::Base 
    belongs_to :country 

class Country < ActiveRecord::Base 
    has_many :states 

我在新的軌道。我想設置成COUNTRY_ID它的狀態,但它給我的錯誤。我認爲我做了一切正確的事,但仍然發生這種錯誤。請幫助我。

回答

1

它的狀態小號(複數);該公司預計,數組

country.states = [first_state] 
+0

感謝兄弟@SLD – Jack 2014-10-04 03:16:48

0

或者你也可以推到各州陣列狀

country.states<< first_state 

此外,由於使用的是固定的表,你可以使用this寶石來對它們進行查詢。如果你喜歡它,請隨時貢獻給寶石。

相關問題