アイテムが7日以上経過しているかどうかを確認するために、ページが読み込まれるたびに(今のところ)フィルターを実行する前にこれを実行し、存在する場合は、属性を更新するためにいくつかのアクションを実行します。
私はbefore_filter :update_it
アプリケーションコントローラーにいます。update_it
以下と同じコントローラーで次のように定義されます。
def update_it
@books = Book.all
@books.each do |book|
book.update_queue
end
end
次にupdate_queue
、本のモデルで定義されます。これに関連するモデルのすべてがここにあります:
scope :my_books, lambda {|user_id|
{:conditions => {:user_id => user_id}}
}
scope :reading_books, lambda {
{:conditions => {:reading => 1}}
}
scope :latest_first, lambda {
{:order => "created_at DESC"}
}
def move_from_queue_to_reading
self.update_attributes(:queued => false, :reading => 1);
end
def move_from_reading_to_list
self.update_attributes(:reading => 0);
end
def update_queue
days_gone = (Date.today - Date.parse(Book.where(:reading => 1).last.created_at.to_s)).to_i
# If been 7 days since last 'currently reading' book created
if days_gone >= 7
# If there's a queued book, move it to 'currently reading'
if Book.my_books(user_id).where(:queued => true)
new_book = Book.my_books(user_id).latest_first.where(:queued => true).last
new_book.move_from_queue_to_reading
currently_reading = Book.my_books(user_id).reading_books.last
currently_reading.move_from_reading_to_list
# Otherwise, create a new one
else
Book.my_books(user_id).create(:title => "Sample book", :reading => 1)
end
end
end
私の関係は、本はユーザーに属し、ユーザーは多くの本を持っているということです。私はこれらの本をユーザーショービューのビューに表示していますが、それは重要ではありません。
だから私が得続けるエラーはそれでmove_from_queue_to_reading
ありmove_from_reading_to_list
、未定義のメソッドです。どうすればいいの?私はそれらを明確に定義し、それからそれらを以下で呼んでいます。私は本当に途方に暮れていて、私が間違っていることについての洞察を大いに感謝します。私はここでは初心者なので、構造化された批判は素晴らしいでしょう:)
編集
取得した正確なエラーメッセージとスタックトレースは次のとおりです。
NoMethodError in UsersController#show
undefined method `move_from_queue_to_reading' for nil:NilClass
app/models/book.rb:41:in `update_queue'
app/controllers/application_controller.rb:22:in `block in update_it'
app/controllers/application_controller.rb:21:in `each'
app/controllers/application_controller.rb:21:in `update_it'