由於某種原因,rubocop窒息了我在我的模型中的代碼,以正確地解決accepts_nested_attributes_for
像查找或創建一樣工作。當我試圖刪除self
的呼叫時,它爆炸了。在此之前,我將專家關閉之前,我正在推遲專家。思考?rubocop風格模型自我需要,但觸發警告
class Job < ActiveRecord::Base
belongs_to :company
before_validation :find_company
accepts_nested_attributes_for :company
private
def find_company
if self.company
self.company = Company.where(email: self.company.email).first_or_initialize
end
end
end
當您使用您必須考慮尋找或創造嵌套屬性由http://stackoverflow.com/questions/3579924/accepts-nested-attributes-for-with-find-or-create = >我在這裏錯過了什麼?這確實修復了rubocop。 –
@chrishough無論如何,至於rubocop - 只有在分配它時才使用'self.attribute',比如'self.age = 25;保存!',其他任何時候它都可以在沒有'self'的情況下工作 –
@chrishough也關於您提供的參考 - 我沒有時間仔細閱讀,但我敢打賭,刪除'before_validation'並添加出席驗證會工作 –