Ruby on rails 为什么FactoryBot每次都为lat/lng分配相同的值?
我的工厂位于以下地点:Ruby on rails 为什么FactoryBot每次都为lat/lng分配相同的值?,ruby-on-rails,factory-bot,Ruby On Rails,Factory Bot,我的工厂位于以下地点: 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
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
不幸的是,这使得位置与采样点相同。如何在每次创建位置时获得随机点
谢谢您可以使用
你可以使用
您可以将
有效\u点移动到工厂:位置do
之前。不需要每次都定义它,rest就可以了。您可以在工厂:位置do
之前移动有效的\u点
。不需要每次都定义它,休息就可以了。
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