something that is perplexing me.. i have the following relationship say: CREATE TABLE `binaries` ( `id` int(11) NOT NULL auto_increment, `data` blob NOT NULL, `datafile_id` int(11) NOT NULL default ''0'', PRIMARY KEY (`id`) ) CREATE TABLE `datafiles` ( `id` int(11) NOT NULL auto_increment, PRIMARY KEY (`id`) ) in datafile.rb I have class Datafile < ActiveRecord::Base has_one :binary, :dependent => true end and in binary.rb i have class Binary < ActiveRecord::Base belongs_to :datafile end now, when i want to create a new datafile object and then inturn add data to the associated ''binary'' object i would like to do this @datafile = Datafile.new @datafile.binary.data = ''blah'' but I CANT. how am i doing this incorrectly? I thought that i could reference the associated object this way. i can not even created the associated object like this either: @datafile = Datafile.new @datafile.binary = Binary.new @datafile.binary.data = ''blah'' there must be an easier way than something like this: @datafile = Datafile.new @datafile.save @binary = Binary.new @binary.datafile_id = @datafile.id @binary.save I feel like i have missed something simple -felix
Julian ''Julik'' Tarkhanov
2005-Oct-05 12:36 UTC
Re: creating new objects with associations
> > now, when i want to create a new datafile object and then inturn add > data to the associated ''binary'' object i would like to do this > > @datafile = Datafile.new > @datafile.binary.data = ''blah'' > > but I CANT. how am i doing this incorrectly? I thought that i could > reference the associated object this way. i can not even created the > associated object like this either: > > @datafile = Datafile.new > @datafile.binary = Binary.new > @datafile.binary.data = ''blah'' > > there must be an easier way than something like this: > > @datafile = Datafile.new > @datafile.save > @binary = Binary.new > @binary.datafile_id = @datafile.id > @binary.save > > I feel like i have missed something simpleMight that be @datafile = Datafile.new @datafile.build_binary(:data=>''blah'') or @datafile.create_binary(:data=>''blah'') -- Julian "Julik" Tarkhanov
Julian ''Julik'' Tarkhanov wrote:>> >> now, when i want to create a new datafile object and then inturn add >> data to the associated ''binary'' object i would like to do this >> >> @datafile = Datafile.new >> @datafile.binary.data = ''blah'' >> >> but I CANT. how am i doing this incorrectly? I thought that i could >> reference the associated object this way. i can not even created the >> associated object like this either: >> >> @datafile = Datafile.new >> @datafile.binary = Binary.new >> @datafile.binary.data = ''blah'' >> >> there must be an easier way than something like this: >> >> @datafile = Datafile.new >> @datafile.save >> @binary = Binary.new >> @binary.datafile_id = @datafile.id >> @binary.save >> >> I feel like i have missed something simple > > Might that be > > @datafile = Datafile.new > @datafile.build_binary(:data=>''blah'') > > or > @datafile.create_binary(:data=>''blah'') >it seems that neither of them do anything, both return ''undefined method'' a quick look at the public methods available to the @file object revealed none usable for this task. any other ideas -felix
I think what you''re after is @datafile = Datafile.new @datafile.binary.data << ''blah'' @datafile.save I haven''t tested this though On 10/6/05, Felix McCoey <felix.mccoey-8PZlF8N9LqR+XZJcv9eMoEEOCMrvLtNR@public.gmane.org> wrote:> > > > Julian ''Julik'' Tarkhanov wrote: > >> > >> now, when i want to create a new datafile object and then inturn add > >> data to the associated ''binary'' object i would like to do this > >> > >> @datafile = Datafile.new > >> @datafile.binary.data = ''blah'' > >> > >> but I CANT. how am i doing this incorrectly? I thought that i could > >> reference the associated object this way. i can not even created the > >> associated object like this either: > >> > >> @datafile = Datafile.new > >> @datafile.binary = Binary.new > >> @datafile.binary.data = ''blah'' > >> > >> there must be an easier way than something like this: > >> > >> @datafile = Datafile.new > >> @datafile.save > >> @binary = Binary.new > >> @binary.datafile_id = @datafile.id <http://datafile.id> > >> @binary.save > >> > >> I feel like i have missed something simple > > > > Might that be > > > > @datafile = Datafile.new > > @datafile.build_binary(:data=>''blah'') > > > > or > > @datafile.create_binary (:data=>''blah'') > > > > it seems that neither of them do anything, both return ''undefined method'' > > a quick look at the public methods available to the @file object > revealed none usable for this task. any other ideas > > -felix > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails