沒有爲模型下面的代碼:如何在單獨的類中移動驗證方法?
class Place < ActiveRecord::Base
attr_accessible :address, :name, :latitude, :longitude
validates :name, :address, :latitude, :longitude, presence: true
etc...
end
並有一些類具有相同的驗證方法:
class PlaceClub
include Virtus
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
attr_reader :club
attr_reader :place
attribute :name, String
attribute :address, String
attribute :latitude, Float
attribute :longitude, Float
validates :name, :address, :latitude, :longitude, presence: true
etc ...
end
我可以搬進爲了使用它驗證方法的單獨的類在這兩個班?提前致謝。
這是可以做到你想要什麼,但它是非常糟糕的設計。如果您使用Rails 4,請考慮使用問題。 – Max
我使用Rails 3.2。這個版本中沒有關注問題,不是嗎? – malcoauri
不,他們不是,但你可以使用模塊做基本相同的事情。我會在一秒之內發佈答案,但我強烈建議不要這樣做。嚴重的是,不要這樣做。 – Max