Skip to content

Instantly share code, notes, and snippets.

View ebrugulec's full-sized avatar
💃

Ebru Gulec ebrugulec

💃
  • Berlin, Germany
View GitHub Profile
> Post.unscoped.order("updated_at desc").limit(10)
Post Load (1.9ms) SELECT `posts`.* FROM `posts` ORDER BY updated_at desc LIMIT 10
> Post.order("updated_at desc").limit(10)
Post Load (17.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`published` = 1 ORDER BY created_at desc, updated_at desc LIMIT 10
> Post.limit(10)
Post Load (3.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`published` = 1 ORDER BY created_at desc LIMIT 10
describe '#delete' do
before do
9.times { create(:customer, type: 'anonym') }
Timecop.freeze(time_parse('01:00'))
load Rails.root.join("lib/tasks/delete_customers.rake")
Rake::Task.define_task(:environment)
end
after do
Timecop.return
namespace :delete do
desc 'delete customers for cron job'
task customers: :environment do
customers = Customer.all
puts "#{customers.count} customers were deleted."
customers.destroy_all
end
end
set :output, {
error: '/log/cron_error_log.log',
standard: '/log/cron_log.log' }
every 1.day, :at => '01:00 am' do
rake "delete:customers"
end
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class Product < ApplicationRecord
has_many :pictures, as: :imageable
end
class Employee < ApplicationRecord
has_many :pictures, as: :imageable
end
class CreatePictures < ActiveRecord::Migration[5.1]
def change
create_table :pictures do |t|
t.string :title
t.references :imageable, polymorphic: true, index: true
t.timestamps
end
end
end