0

I thought this would be a simple task, but I'm finding it difficult to make Rails do what I want.

I've got an array of dates.

So I thought that something like this would work:

  def index
    @datetimes = Books.all.map(&:checkouts).flatten.map(&:out_date)
    @datetimes.each do |c|
      c.to_date
    end
  end

Then I can just call this in my view:

%ul
-@datetimes.each do |c|
    %li=c

How do I modify each key in the array? What am I missing here?

Thanks, so much for being nice to new, novice, and ignorant hobbyists like myself.

4

1 回答 1

1

.each不修改调用者。它只是循环通过。您可以将控制器操作更改为:

@datetimes = Books.all.map(&:checkouts).flatten.map{|e| e.out_date.to_date}

您可能还想探索在您的 Books 查询中包含 :checkouts 以避免 N+1 查询。或者也许做这样的事情。

Checkout.where("book_id is not null").map{|e| e.out_date.to_date}
于 2013-06-11T20:30:11.913 回答