Tuesday, 15 February 2011

ruby on rails - Why is .each only going through the first element? -



ruby on rails - Why is .each only going through the first element? -

i wrote method:

def create_demo_organizations sample_organizations = [ '37 signals', 'fog creek'] sample_organizations.each { |item| organization.first_or_create( name: item, time_zone: 'central' ) } end

i hoping create 2 organizations names in array me, when opened in ui admin tool can see first row '37 signals' , not sec row. did write wrong?

my goal iterate through members of array , insert them in database.

first seek isolate active record code loop code. start with:

sample_organizations.each |item| puts item end

if you're getting both items printed, add together simpler ar call:

sample_organizations.each |item| organization.create(name: item) end

then finally:

sample_organizations.each |item| organization.find_or_create(name: item) end

edit:

i don't think you're calling first_or_create correctly. want this:

organization.where(name: item).where(time_zone: 'central').first_or_create

ruby-on-rails ruby

No comments:

Post a Comment