当我创建记录时,我有rake文件,对于作为引用的字段,它返回nil
namespace :winner do
desc "Generate sitemap"
task :create_winner => :environment do
binding.pry
date = DateTime.now.utc
likes = Like.where('created_at >= ? and created_at <= ?', 2.week.ago.beginning_of_week, 1.week.ago.end_of_week).select(:selfy_id)
top = Selfy.where(id: likes)
@user = top.order("COALESCE(likes_count, 0) DESC").limit(1).pluck(:user_id)
@selfy = top.order("COALESCE(likes_count, 0) DESC").limit(1).pluck(:id)
Winner.create(
date: Time.now,
user_id: @user,
selfy_id: @selfy
)
end
end
答案 0 :(得分:0)
您应该使用id
或使用association
Winner.create(date: Time.now, user: @user, selfy: @selfy)
或
Winner.create(date: Time.now, user_id: @user.id, selfy_id: @selfy.id)