这个问题显然是微不足道的,但我是 Rails 的新手,我不知道我哪里弄错了。我正在从旧电子表格中填充相关模型。下面是一个 rake 导入任务片段。不知何故,当位置(华盛顿)相同并且已存在于 Smith 帖子的数据库中时,为第二条记录所做的新分配post.locations << location
会删除与 Jones 帖子的位置关联。我的想法是将同一个位置与两个人的帖子相关联。我错过了什么?
导入.rake
data = [
{ name: 'Jones',
post: 'President',
city: 'Washington'
},
{ name: 'Smith',
post: 'Vice-President',
city: 'Washington'
},
{ name: 'Peters',
post: 'Janitor',
city: 'New York'
}
]
data.each do |row|
name = row[:name]; post = row[:post]; city = row[:city]
person = Person.where(name: name).first_or_create
post = Post.where(post: post).first_or_create
location = Location.where(city: city).first_or_create
post.people << person
post.locations << location
location.save; person.save; post.save
end
上面的导入导致
person1 = Person.find_by_name("Jones");
person1.posts.first.locations.first == nil
person2 = Person.find_by_name("Smith");
person2.posts.first.locations.first.city == "Washington"
person3 = Person.find_by_name("Peters");
person3.posts.first.locations.first.city == "New York"
位置.rb
class Location < ActiveRecord::Base
belongs_to :post
attr_accessible :city
end
人.rb
class Person < ActiveRecord::Base
attr_accessible :name
has_many :occupations
has_many :posts, through: :occupations
end
post.rb
class Post < ActiveRecord::Base
attr_accessible :post
has_many :occupations
has_many :people, through: :occupations
has_many :locations
end
职业.rb
class Occupation < ActiveRecord::Base
belongs_to :person
belongs_to :post
attr_accessible :person_id, :post_id, :since, :till
end