Ruby 使用创建新的Datamapper资源(数据库条目)。每个
我想获取Google自定义搜索API结果,并使用Datamapper将其添加到数据库中 我已经成功地设置并手动向数据库中添加了一些项目,所以看起来所有设置都是正确的 我使用HTTParty调用googleapi,它返回JSON结果。然后,我想使用JSON并将每个链接添加到数据库中。我正在尝试使用。每一个如下Ruby 使用创建新的Datamapper资源(数据库条目)。每个,ruby,sinatra,datamapper,ruby-datamapper,httparty,Ruby,Sinatra,Datamapper,Ruby Datamapper,Httparty,我想获取Google自定义搜索API结果,并使用Datamapper将其添加到数据库中 我已经成功地设置并手动向数据库中添加了一些项目,所以看起来所有设置都是正确的 我使用HTTParty调用googleapi,它返回JSON结果。然后,我想使用JSON并将每个链接添加到数据库中。我正在尝试使用。每一个如下 response["items"].each do |item| i=DMapperModel.create(city: "London", link: item["link"])
response["items"].each do |item|
i=DMapperModel.create(city: "London", link: item["link"])
puts i
puts i.saved?
end
Response是一个包含HTTParty::Response的变量,“items”和“link”都是HTTParty::Response的子集
放置i
成功放置正确的数据映射器资源(即
)
put i.saved?
是检查i
是否保存到数据库,此时返回false
因此,它成功地将i
设置为DataMapper资源,但由于某种原因没有将其保存到数据库中,有人能看到我的错误吗?自己解决了它
我将links参数设置为字符串,DataMapper将其默认最大长度设置为50个字符。我尝试添加的所有链接都超过50个字符,因此保存失败
通过将link参数的最大长度设置为2000个字符,解决了该问题
class CityLink
include DataMapper::Resource
property :id, Serial
property :city, String
property :link, String, *length: 2000*
end
我从DataMapper文档中获取了2000个字符的长度,用于URI参数类型和指向的链接
一旦我设置了这个,上面提到的每种方法都起了作用