2009-09-21 2 views
0

에 더 많은 문제가 있음이 (가) user_ids을 사용하여 여러 연결을 통해 발생했습니다.Ruby on Rails가 배열을 통해 많은 문제가 있음

내 통신 모델은 다음과 같습니다 : 내가 할 수있는

@communication = new Communications(params[:communication]) 
@communication.user_ids << id 
logger.debug @communication.user_ids # is empty 

'내가 수동과 같이 통신 객체에 user_ids를 추가하려고하고있는 통신 컨트롤러 내 생성 작업에

class communication 
has_many :recipients 
has_many :users, :through => :recipients 
end 

왜 이런 식으로 하드 코드 된 ID를 할 때라도 @communication.user_ids 배열이 비어있는 이유는 무엇입니까?

@communication = new Communications(params[:communication]) 
@communication.user_ids << 1 
logger.debug @communication.user_ids # is still empty! 

아직 배열이 비어 있습니다. @communication.user_ids.

내 방법으로 뭔가가 누락 되었습니까? 이 작업을 수행하는 데 도움이되는 팁이 있습니까?

미리 감사드립니다.

답변

2

has_many :through이므로 관계를 원활하게 만들 수 있도록 전체 개체를 제공해야 할 수도 있습니다. 사용해보기 :

@communication = Communication.new params[:communication] 
@communication.users << User.find(1) 
@communication.user_ids # should be [ 1 ] 
관련 문제