Try to search your question here, if you can't find : Ask Any Question Now ?

Rails function be called two times with different argument but the data in both return is the same

HomeCategory: stackoverflowRails function be called two times with different argument but the data in both return is the same
julie asked 3 weeks ago

As the code I wrote, I tried to make arrangement the data of two shops
there are two object I declared

@period = Date.new(2019,3,1)..Date.new(2019,3,31)

@deals_1 = Deal.includes(:staff).where(shop_id:1, created_at:@period).group_by{|t| t.staff}

@deals_2 = Deal.includes(:staff).where(shop_id:2, created_at:@period).group_by{|t| t.staff}

and I used a function computed_deals to wrap it up, and let both of them become the same structure with different data in the container.

def computed_deals(deals, period ,shop_id)
 @data = {}
 @staff_id_container = {}

 Staff.where(shop_id: shop_id).select(:id).each do |staff|
  @staff_id_container[staff.id] = {item1:0, item2:0, item3:0}
 end

 period.each do |date|
  @data[date] = @staff_id_container.dup
 end

 deals.each do |staff, deals_by_staff|
  deals_by_staff.group_by{|t| t.created_at.to_date}.each do |date, selected_deals|
   @data[date][staff.id] = {
    item1: select_deals.select{|deal| deal.item == "item1"}.count
    item2: select_deals.select{|deal| deal.item == "item2"}.count
    item3: select_deals.select{|deal| deal.item == "item3"}.count
   }
  end
 end
 return @data
end

Theoretically speaking, when I called a function with different argument should return different data with the same structure, but @data_1 and @data_2 are totally the same.

@data_1 = computed_deals(@deals_1, @period, 1)
@data_2 = computed_deals(@deals_2, @period, 2)
puts @data_1 == @data_2 #true

what wrong does it happen?

1 Answers
Best Answer
Jyoti answered 3 weeks ago
Your Answer

19 + 12 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com