nested_attributes를 사용한 콜백 전
-
22-07-2019 - |
문제
다음과 같이 주어진다 :
Foo has_many :bars, :through => :baz
그리고
Foo accepts_nested_attributes_for :bar
나는하고 싶다 find_or_create_by_name
새를 추가 할 때 :bar
,하지만 나는 내가 어디에 어떤 종류를 가질 수 있는지 모르겠다 before_add
기능.
이 질문의 배경은입니다 Bar validates_uniqueness_of :name
, 새로운 것을 만들려고 할 때 오류가 발생합니다. Foo
그것은 기존을 사용하고 있습니다 Bar
.
해결책
와우 나는 피곤해야한다 :
class Foo < ActiveRecord::Base
has_many :bars, :through => :baz, :before_add => :some_callback
def some_callback(b)
#whatnot
end
end
그러나 여전히 some_callback
부분, 무엇을해야합니까? 나는 같은 것을 시도했다 b = Bar.find_or_create_by_name(b.name)
그러나 그것은 작동하지 않습니다.
제휴하지 않습니다 StackOverflow