Hey, trying to insert a record to a habtm table called devices_oids. it has 2 fields , device_id, and oid_id. i Have the following code and i am getting a NIL error when i try to "SAVE" the object. @oid = @params[:oid_id].to_i @deviceid = @params[:device_id].to_i @entry = DevicesOid.new @entry.device_id = @deviceid @entry.oid_id = @oid_id if @entry.save flash[:notice] = ''Successfully Added OID to Device.'' else flash[:notice] = ''Something went wrong .'' end any ideas what is wrong here ? thanks adam
On Mon, 2005-31-10 at 13:36 -0500, Adam Denenberg wrote:> Hey, > > trying to insert a record to a habtm table called devices_oids. it > has 2 fields , device_id, and oid_id. i Have the following code and i am > getting a NIL error when i try to "SAVE" the object. > > @oid = @params[:oid_id].to_i > @deviceid = @params[:device_id].to_i > @entry = DevicesOid.new > @entry.device_id = @deviceid > @entry.oid_id = @oid_id > if @entry.save > flash[:notice] = ''Successfully Added OID to Device.'' > else > flash[:notice] = ''Something went wrong .'' > end > > any ideas what is wrong here ? > > thanks > adam > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsAdam, Try something like this: device = Device.find(@params[:device_id]) oid = Oid.find(@params[:oid_id]) device.oids << oid device.save In a regular habtm relation, no joining model is created, nor should you create one, so the DevicesOid thing you have just won''t work. It would if you made it a full-fledged model acting as the middleman, but that''s probably overkill. - Jamie
worked like a charm, thank you. I thought I still had to associate a model with a HABTM join table in order to get data into it. I should have known with rails i was overcomplicating the matter. thanks again adam Jamie Macey wrote:> On Mon, 2005-31-10 at 13:36 -0500, Adam Denenberg wrote: > >>Hey, >> >> trying to insert a record to a habtm table called devices_oids. it >>has 2 fields , device_id, and oid_id. i Have the following code and i am >>getting a NIL error when i try to "SAVE" the object. >> >> @oid = @params[:oid_id].to_i >> @deviceid = @params[:device_id].to_i >> @entry = DevicesOid.new >> @entry.device_id = @deviceid >> @entry.oid_id = @oid_id >> if @entry.save >> flash[:notice] = ''Successfully Added OID to Device.'' >> else >> flash[:notice] = ''Something went wrong .'' >> end >> >> any ideas what is wrong here ? >> >>thanks >>adam >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > Adam, > > Try something like this: > > device = Device.find(@params[:device_id]) > oid = Oid.find(@params[:oid_id]) > device.oids << oid > device.save > > In a regular habtm relation, no joining model is created, nor should you > create one, so the DevicesOid thing you have just won''t work. It would > if you made it a full-fledged model acting as the middleman, but that''s > probably overkill. > > - Jamie > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails