Rails 验证虚拟属性
我的模型如下所示:
class Item < ActiveRecord::Base
has_many :locations
validate :validate_item_location
def item_location
locations.address+','+locations.city+','+locations.country
end
def item_location=(str)
geo = Geokit::Geocoders::MultiGeocoder.geocode(str)
if geo.success
locations.build( :lat => geo.lat, :lng => geo.lng)
end
end
def validate_item_location
geo = Geokit::Geocoders::MultiGeocoder.geocode( item_location )
errors.add_to_base("Location is invalid") unless geo.success
end
end
我的问题 1.如何正确编写item_location定义的getter方法? 2. 如何验证 item_location 字段。我创建了 validate_item_location 方法,但不知道当我通过表单发布数据时如何在内部获取 item_location 变量。 3.我的setter方法可以吗?
谢谢!
My model looks like this:
class Item < ActiveRecord::Base
has_many :locations
validate :validate_item_location
def item_location
locations.address+','+locations.city+','+locations.country
end
def item_location=(str)
geo = Geokit::Geocoders::MultiGeocoder.geocode(str)
if geo.success
locations.build( :lat => geo.lat, :lng => geo.lng)
end
end
def validate_item_location
geo = Geokit::Geocoders::MultiGeocoder.geocode( item_location )
errors.add_to_base("Location is invalid") unless geo.success
end
end
My questions
1. How to correctly write a getter method item_location defined?
2. How can I validate item_location field. I created validate_item_location method but don't know how to get item_location variable inside when I POST data through my form.
3. Is my setter method ok?
THX!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)一个Item可以有多个位置吗? (在我看来)它应该只有一个,因此将
hasy_many
更改为has_one
。除非您确实想要拥有多个位置,否则您需要更改item_location
以从您拥有的列表中选择一个位置。2& 3) 如果您通过表单发布数据,则 item_location 将通过
item_location=
方法设置。哪个应该(以某种方式)存储项目信息。在您的情况下,它存储从geo
变量返回的坐标。当 geo.success 为 false 时,您应该引发一些错误以通知用户该值未存储。如果您特别想验证发送到 setter 的值,那么您需要将其存储在类中:@saved_location = str
并使用@saved_location
进行验证,而不是项目_位置。1 & 3)通常情况下,setter 和 getter 使用相同的数据(结构)。在您的情况下,您将位置的坐标存储在设置器中,但返回地址、城市和国家/地区。因此 setter 和 getter 似乎是不兼容的。
希望这些言论有所帮助!
1) An Item can have many locations? It seems (to me) that it should have only one, so change
hasy_many
tohas_one
. Unless you really wanted to have multiple locations, then you need to changeitem_location
to choose one location from the list you have.2 & 3) If you POST your data through a form, the item_location gets set by the
item_location=
method. Which should (somehow) store the item information. In your case it stores the coordinates returned from thegeo
variable. You should raise some error, whengeo.success
is false to notify the user that the value was not stored. If you specifically want to validate the value send to the setter, then you need to store it in the class:@saved_location = str
and use@saved_location
to validate, instead of item_location.1 & 3) In general it is practice that, a setter and a getter use the same data(structure). In your case you store the coordinates of the position in your setter, but give back the address, city and country. Thus the setter and the getter seem to be incompatible.
Hope these remarks help!