'Params changing when calling a method
I have params with "start_date", "end_date" and "filter"
Before calling this line, params[:start_date] is, for example, "2022-02-07" and end_date is "2022-02-09". So far so good.
format.csv{send_data @records.to_csv(params[:start_date], params[:end_date], params[:filter]), filename: "fileName.csv"}
After calling that line, inside the method 'self.to_csv(...)', if I print the values of start_date now it is the same as end_date.
def self.to_csv(start_date, end_date, filter)
binding.pry # Here, I print the values of the params and they changed for some reason.
...
end
I tried to pass @ records, convert @ records to an array and use that, and even tried to pass params. Every single time, the argument :start_date changes.
Complete (and relevant) model and controller code: record.rb
def self.to_csv(start_date, end_date, filter)
titles = %w(title1 title2 title3)
attributes = %w(attr1 attr2 attr3)
CSV.generate("\uFEFF", headers: true) do |csv|
csv << titles
all.each do |record|
csv << attributes.map{ |attr| record.send(attr) }
end
end
end
records_controller.rb
def index
if params[:year].present?
@delayed_records = Record.unscoped.delayed.not_current_year(params[:year])
@records = Record.unscoped.getFilters(params[:start_date], params[:end_date], params[:filter]).not_current_year(params[:year])
else
@delayed_records = Record.delayed
@records = Record.getFilters(params[:start_date], params[:end_date], params[:filter])
end
start_date = params[:start_date]
end_date = params[:end_date]
filter = params[:filter]
# Everything works up until this point. Params are OK
respond_to do |format|
format.html
format.csv { send_data @records.to_csv(start_date, end_date, filter), filename: "fileName.csv" }
end
end
To simplify this concept, imagine this situation:
methodA(num){
print(num)
}
methodA(5)
// Output -> Prints 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
