Hi everyone. I´m working on application that has a "has_and_belongs_to_many" betwen two models: "expedientes" and "documentos". When I´m on the console, I need to pass a Expedientes Array to documentos, otherwise I get a ActiveRecord::AssociationTypeMismatch exception. Example:>> d=Documento.new=> #<Documento id: nil...>> d.expedientes=[1]ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected, got Fixnum(#108610) ...>> d.expedientes="1"ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected, got String(#113650) ...>> d.expedientes=Expediente.find("1")NoMethodError: undefined method `each'' for #<Expediente:0x23a96a0> ...>> d.expedientes=[Expediente.find("1")]=> [#<Expediente id: 1, ...That´s ok! How must I made this relationship from the view? I´ve mading a Form.hidden_field from the view "new" with the name "expedientes" and the value of the expediente.id of the relationship,but seems incorrect. What´s the best way to do that?
The best way (for me) is using nested attributes (http:// api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ ClassMethods.html). You can look a very complete example of usage in: http://github.com/alloy/complex-form-examples/tree/master and in http://github.com/soldieron/jquery-nested-forms/tree/master Regards. Franco Catena. On 5 jun, 13:49, Jose Ernesto Suarez <suarez.erne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi everyone. > > I´m working on application that has a "has_and_belongs_to_many" betwen > two models: "expedientes" and "documentos". > When I´m on the console, I need to pass a Expedientes Array to > documentos, otherwise I get a ActiveRecord::AssociationTypeMismatch > exception. > > Example:>> d=Documento.new > > => #<Documento id: nil...>> d.expedientes=[1] > > ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected, > got Fixnum(#108610) > ...>> d.expedientes="1" > > ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected, > got String(#113650) > ...>> d.expedientes=Expediente.find("1") > > NoMethodError: undefined method `each'' for #<Expediente:0x23a96a0> > ...>> d.expedientes=[Expediente.find("1")] > > => [#<Expediente id: 1, ...That´s ok! > > How must I made this relationship from the view? > I´ve mading a Form.hidden_field from the view "new" with the name > "expedientes" and the value of the expediente.id of the > relationship,but seems incorrect. > > What´s the best way to do that?
Thanks Franco... On 6 jun, 02:31, Franco Catena <francocat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The best way (for me) is using nested attributes (http:// > api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ > ClassMethods.html). You can look a very complete example of usage in:http://github.com/alloy/complex-form-examples/tree/masterand inhttp://github.com/soldieron/jquery-nested-forms/tree/master > > Regards. > > Franco Catena. > > On 5 jun, 13:49, Jose Ernesto Suarez <suarez.erne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi everyone. > > > I´m working on application that has a "has_and_belongs_to_many" betwen > > two models: "expedientes" and "documentos". > > When I´m on the console, I need to pass a Expedientes Array to > > documentos, otherwise I get a ActiveRecord::AssociationTypeMismatch > > exception. > > > Example:>> d=Documento.new > > > => #<Documento id: nil...>> d.expedientes=[1] > > > ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected, > > got Fixnum(#108610) > > ...>> d.expedientes="1" > > > ActiveRecord::AssociationTypeMismatch: Expediente(#18729340) expected, > > got String(#113650) > > ...>> d.expedientes=Expediente.find("1") > > > NoMethodError: undefined method `each'' for #<Expediente:0x23a96a0> > > ...>> d.expedientes=[Expediente.find("1")] > > > => [#<Expediente id: 1, ...That´s ok! > > > How must I made this relationship from the view? > > I´ve mading a Form.hidden_field from the view "new" with the name > > "expedientes" and the value of the expediente.id of the > > relationship,but seems incorrect. > > > What´s the best way to do that?