开发者

Rails date_select to generate a csv file

开发者 https://www.devze.com 2023-03-19 16:52 出处:网络
I\'m trying to export a CSV file of Newbie user email addresses. Newbies are users \"created_at\" after the date selected in the date_select tag (basically the most recent sign-ups).

I'm trying to export a CSV file of Newbie user email addresses. Newbies are users "created_at" after the date selected in the date_select tag (basically the most recent sign-ups).

I'm using the FasterCSV gem and Scott Becker's code, and borrowed the @start_date from here to implement in the method.

Here's what my controller looks like:

  def newbs_export
    @start开发者_运维百科_date = Date.civil(params[:range][:"start_date(1i)"].to_i, params[:range][:"start_date(2i)"].to_i, params[:range][:"start_date(3i)"].to_i)
    @users = User.find(:all, :if => [@created_at >= @start_date])

    csv_string = FasterCSV.generate do |csv|
      # header row
      csv << ["email"]

      # data rows
      @users.each do |user|  
      #if user[:created_at] >= Time.now
        csv << [user.email]
      end

    end

    # send it to the browsah
    send_data csv_string,
              :type => 'text/csv; charset=iso-8859-1; header=present',
              :disposition => "attachment; filename=newbs.csv"

  end

Here's my newbs.html.erb:

<% form_tag({:controller => "accounts", :action => "newbs_export"}, :method => "get") do %>
<%= date_select('range', 'start_date', :order => [:month, :day, :year])%>
<%= submit_tag("Submit") %>
<% end %>

I've mixed code and dug myself a bit of a hole trying to stitch different solutions together. Any advice on where I could go from here?

Thanks!


I will start with changing:

@users = User.find(:all, :if => [@created_at >= @start_date])

to

@users = User.where("created_at >= ?", @start_date)

For rails3 or

@users = User.find(:all, :conditions => ["created_at >= ?", @start_date])

For rails2

If you post any other errors / problems you are having I will try to update this answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消