Ruby on Rails에는 배열 문제가 많이 있습니다
-
12-09-2019 - |
문제
나는 추가하는 데 문제가 있습니다 많은 것을 가지고 있습니다 협회 사용 user_ids
.
내 커뮤니케이션 모델은 다음과 같습니다.
class communication
has_many :recipients
has_many :users, :through => :recipients
end
통신 컨트롤러에 대한 조치에서 수동으로 추가하려고합니다. user_ids
그렇게하는 커뮤니케이션 객체에 :
@communication = new Communications(params[:communication])
@communication.user_ids << id
logger.debug @communication.user_ids # is empty
나는 이유를 해결할 수 없다 @communication.user_ids
배열이 비어 있습니다.
@communication = new Communications(params[:communication])
@communication.user_ids << 1
logger.debug @communication.user_ids # is still empty!
나는 아직도 비어있다 @communication.user_ids
정렬.
내 방법으로 뭔가를 놓치고 있습니까? 이것을 작동시키기위한 팁이 있습니까?
미리 감사드립니다!
해결책
a has_many :through
, 아마도 관계가 부드럽게 만들 수 있도록 전체 객체를 제공해야 할 수도 있습니다. 이 시도:
@communication = Communication.new params[:communication]
@communication.users << User.find( 1 )
@communication.user_ids # should be [ 1 ]
제휴하지 않습니다 StackOverflow