与自定义ActiveModel :: Validator一起使用时,find_or_create_by失败

时间:2015-11-06 08:55:55

标签: ruby-on-rails-4

我有两个名为Drug and Frequency的模型。

use std::cell::*;

struct Parent<'a> {
    inner: Option<RefMut<'a, Object>>,
}
impl<'a> Parent<'a> {
    fn new() -> Parent<'a> {
        Parent {
            inner: None,
        }
    }
    fn bind(&mut self, obj: &'a RefCell<Object>) {
        self.inner = Some(obj.borrow_mut());
    }
    fn unbind(&mut self) {
        self.inner = None;
    }
}
struct Object {
    inner: u32,
}
fn main() {
    let obj = RefCell::new(Object { inner: 0 });
    let mut parent1 = Parent::new();
    parent1.bind(&obj);
    parent1.unbind(); // comment this line out and you get a runtime panic
    let mut parent2 = Parent::new();
    parent2.bind(&obj);
}

即使从系统中获取记录,也会执行自定义DrugValidator。我如何为find_or_create_by避免这种情况?

1 个答案:

答案 0 :(得分:0)

指定此验证何时生效。

类药物   validates_with DrugValidator,if:(lambda {| drug | drug.name.present?}),on :: create as 端