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>**
在模特:
class State < ActiveRecord::Base
belongs_to :country
class Country < ActiveRecord::Base
has_many :states
我是铁杆新手。我试图将country_id设置为它的状态,但它给了我错误。我认为我做的一切都很正确,但仍然会发生这种错误。请帮帮我。
答案 0 :(得分:1)
状态 s (复数);它期待一个数组
country.states = [first_state]
答案 1 :(得分:0)