Rails 验证虚拟属性

发布于 2024-08-17 19:31:19 字数 752 浏览 3 评论 0原文

我的模型如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2024-08-24 19:31:19

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 to has_one. Unless you really wanted to have multiple locations, then you need to change item_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 the geo variable. You should raise some error, when geo.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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文