current_user.posts.do_changes_and_return_collection
대신
Post.where(user_id: current_user.id).do_changes_and_return_collection
의 수행 및 방법 내부 컬렉션 작업을 할 수 있습니까?
current_user.posts.do_changes_and_return_collection
대신
Post.where(user_id: current_user.id).do_changes_and_return_collection
의 수행 및 방법 내부 컬렉션 작업을 할 수 있습니까?
을 참조하십시오 :
class Post < ActiveRecord::Base
scope :do_changes_and_return_collection, -> { where(active: true) }
end
당신은 호출 할 수 있습니다
def self.do_changes_and_return_collection
update_all(deleted: false) # Just an example.
where(submited: true)
end
을 그리고 당신은 호출 할 수 submiteds = current_user.posts.do_changes_and_return_collection
를
참고 : update_all
가 실행되지 않습니다 검증 및 콜백
편집 :
has_many :posts do
def owner
proxy_association.owner
end
end
그리고 전화 current_user.posts.owner
당신이 CURRENT_USER 얻을 것이다 : 당신이 has_many 관계를 확장해야합니다 User
에서
관계 소유자를 얻으려면
예, 가능합니다. 따라서 scope
을 사용해야합니다.
Ex.
이Post.where(user_id: current_user.id).do_changes_and_return_collection
난 당신이 게시물 모델 같은 것을 할 생각 scopes
메서드 내에서 컬렉션을 보유하는 부모 개체를 가져 오는 ActiveRecord 방법이 있는지 알고 있습니까 (즉, 'current_user')? 'self.first.user' 작품으로 추측됩니다. – Numbers
@MsNumbers 내 편집 확인 – Doguita
정말 놀라워요, 고마워요! – Numbers