Monday, 15 September 2014

ruby on rails - resource id is nil after create - why? -



ruby on rails - resource id is nil after create - why? -

so i've got in seeds.rb

15.times topic = topic.create( name: faker::lorem.word, ) color = topic.color_topic topic.update_attributes(color:color) end

which calls in model

def color_topic # distributes color-0 thru color-4 as through topics puts self.id "color-#{self.id % 5}" end

and maintain getting stuff this

122 123 124 125 126 127 128 129 130 rake aborted! nomethoderror: undefined method `%' nil:nilclass

so ruby running ahead of activerecord's assigning of id? thought type of logic used time. missing?

in terms of seeding info have uniqueness constraints, wouldn't recommend leaving random chance (ie. faker::lorem). it's much easier like:

(1..15).each |i| topic = topic.create(name: "name#{i}") end

also, sidenote, may want using mill girl seed info it's far more powerful out of box seeding framework: https://github.com/thoughtbot/factory_girl

ruby-on-rails

No comments:

Post a Comment