change query more fit to rails
fix error in unit test because of notice
move callback function to observer