我希望能够在我的数据库中放置条目,其中制造商将被多次代表但不是制造商和型号的相同组合.所以"索尼(制造商),电视(型号)"还可以"索尼(制造商),OtherTv(型号)",但第三个条目"索尼(制造商),电视(型号)"自制造商和型号组合以来并不合适不是唯一的.我尝试了:key => true验证,但似乎没有用.而且我不能像validates_uniqueness_of :manufacturer AND :model我猜的那样做.你是怎么做到的?
class Tvs
include DataMapper::Resource
property :id, Serial
property :manufacturer, String, :key => true
property :model, String, :key => true
validates_uniqueness_of :
end
Run Code Online (Sandbox Code Playgroud) 我有一个带有一系列链接的ruby类.因为现在我可以保存Paper对象,即使该数组包含的链接不是有效的URL.我有一个方法贯穿数组并验证网址,如果网址无效,则返回false.但是当我尝试调用Paper.save时,我想收到一条错误消息.那可能吗?
class Paper
include MongoMapper::Document
key :links, Array
validates_presence_of :links
def validate_urls
reg = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
status = []
links.each do |link|
if link.match(reg)
status.push('true')
else
if "http://#{link}".match(reg)
status.push('true')
else
status.push('false')
end
end
end
if status.include?('false')
return false
else
return true
end
end
end
Run Code Online (Sandbox Code Playgroud)