ruby on rails - Calling Model name on it's model -
i have import feature excel. , put on model which:
def self.import(file, employee_name) spreadsheet = open_spreadsheet(file) header = spreadsheet.row(1) (2..spreadsheet.last_row).each |i| row = hash[[header, spreadsheet.row(i)].transpose] category = category.where(:name => row["category"]).last if category.blank? category = category.create(:name => row["category"], :is_active => 1) end unit = unitofmeasure.where(:name => row["unit"]).last if unit.blank? unit = unitofmeasure.create(:name => row["unit"], :is_active => 1) end chart_of_account_id=0 stock_output_account=0 if row["can sold"]==1 income_account=1 else income_account=0 end if row["can purchased"]==1 expense_account=1 else expense_account=0 end product = product.create(:plu => row["plu"], :plu_night_disc => row["plu night disc."], :name => row["item desc."], :min_stock => ["min. stock"], :product_type => row["product type"], :notes => ["notes"], :sales_price => ["sales price"], :night_disc_price => ["night disc. price"], :bottom_price => ["bottom price"], :category_id => category.id, :unit_of_measure_id => unit.id, :chart_of_account_id => chart_of_account_id, :stock_output_account => stock_output_account, :income_account => income_account, :expense_account => expense_account, :can_be_sold => row["can sold"], :can_be_purchased => row["can purchased"], :employee_name => employee_name, :is_active => 1) end end
but when import, doesn't return error, creation of product
skipped (look long code), when try change product
example model country
inserting database finely. confuse of behaviour. please help. thanks
this behavior you're getting means have invalid product
record, , insertion failing silently. try using create!
method instead:
product = product.create!(...)
this method raise error if model invalid, explanations on why. can use information debug code.
hope helps.
Comments
Post a Comment