RailsCasts Pro episodes are now free!

Learn more or hide this

Said Kaldybaev's Profile

GitHub User: Saidbek

Site: http://www.notes.kloop.kg

Comments by Said Kaldybaev

Avatar

Mohamed Sami there're 2 ways to pass your issue

just use collect instead of each

ruby
def load_imported_products
  spreadsheet = open_spreadsheet
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).collect do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    product = Product.find_by_id(row['id']) || Product.new
    product.attributes = row.to_hash.slice(*accessible_attributes)
    product
  end
end

try adding each product to an array, then just return it

ruby
def load_imported_products
  spreadsheet = open_spreadsheet
  header = spreadsheet.row(1)
  products = []
  (2..spreadsheet.last_row).collect do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    product = Product.find_by_id(row['id']) || Product.new
    product.attributes = row.to_hash.slice(*accessible_attributes)
    products << product
  end
  products
end