我有以下工厂所在地:
FactoryBot.define do
factory :location do
valid_points = [
# a bunch of points in format ["lng", "lat"]
]
point = valid_points.sample
geography = RGeo::Geographic.spherical_factory(srid: 4326).point(point[0], point[1])
street_1 { Faker::Address.street_address }
street_2 { Faker::Address.secondary_address }
city { Faker::Address.city }
state { Faker::Address.state }
country { Faker::Address.country }
lonlat { geography }
longitude { point[0] }
latitude { point[1] }
end
end
不幸的是,这会使位置与采样点相同。每次创建位置时如何获得随机点?
谢谢
答案 0 :(得分:2)
您可以使用FactoryBot callbacks
FactoryBot.define do
factory :location do
street_1 { Faker::Address.street_address }
street_2 { Faker::Address.secondary_address }
city { Faker::Address.city }
state { Faker::Address.state }
country { Faker::Address.country }
after(:build) do |location|
valid_points = [
# a bunch of points in format ["lng", "lat"]
]
point = valid_points.sample
geography = RGeo::Geographic.spherical_factory(srid: 4326).point(point[0], point[1])
location.lonlat = geography
location.latitude = point[1]
location.longitude = point[0]
end
end
end