0

私は 1:n (人と都市) の関係を持っています

モデルパーソン:

 belongs_to :city
 composed_of :city, :mapping => %w(city_name city)

モデル都市:

  has_many :people

これで都市を設定できるはずです: peson.city = "London" ? 私はそれを理解していますか、それとも目的は何ですか?

http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html

この例では、has_many や belongs_to を使用していません。なんで?

4

1 に答える 1

0

あなたがするとき

person.city = "London"

set city to the string 'London'Rails はこれを正しくないと見なします。Railsに伝えたいことはset the city to the record in the database with a name of 'London'. あなたは(最も簡単な言葉で)それを行います

london = City.find_by_name 'London'
person.city = london
于 2013-03-01T06:29:37.320 に答える