Ruby on Rails hat viele Durch Array Problem
-
12-09-2019 - |
Frage
Ich habe Probleme Zugabe zu einer hat viele Durch Assoziation mit user_ids
.
Meine Kommunikationsmodell sieht wie folgt aus:
class communication
has_many :recipients
has_many :users, :through => :recipients
end
In meiner erstellen Aktion für die Kommunikationssteuerung des ich versuche manuell user_ids
auf das Kommunikationsobjekt hinzufügen, etwa so:
@communication = new Communications(params[:communication])
@communication.user_ids << id
logger.debug @communication.user_ids # is empty
Ich kann nicht arbeiten, warum @communication.user_ids
Array leer ist, auch wenn ich eine hart codierte ID tun wie folgt:
@communication = new Communications(params[:communication])
@communication.user_ids << 1
logger.debug @communication.user_ids # is still empty!
Ich bin immer noch ein leeres @communication.user_ids
Array zu bekommen.
Fehle ich etwas mit meiner Methode? Irgendwelche Tipps diese Arbeit zu bekommen?
Vielen Dank im Voraus!
Lösung
Da es ein has_many :through
ist, vielleicht brauchen Sie volle Objekte zu liefern, so dass die Beziehung reibungslos erstellt werden kann. Versuchen Sie folgendes:
@communication = Communication.new params[:communication]
@communication.users << User.find( 1 )
@communication.user_ids # should be [ 1 ]