我正在尝试使用where
方法执行记录搜索,其中有两个条件,其中一个条件必须为零。Rails在属性为零的地方查找记录
我使用Rails 4,Ruby 2.0.0和Postgres。
我有一个应用程序与三个模型:用户,Drm和DrmMorning。用户有许多Drm,而Drm有许多DrmMornings。我正在尝试隔离一个DrmMorning记录,以便我可以对其进行编辑。
这是我drm_mornings控制器更新操作:
def update
@user = current_user
@drm = Drm.where(user_id: @user.id).last
@drm_mornings = DrmMorning.where(" drm_id = ? AND location = ?", @drm.id, nil).first || DrmMorning.where(" drm_id = ? AND happy = ?", @drm.id, nil).first
if @drm_mornings.nil?
flash[:error] = "thanks for finishing up"
render 'drm/intro'
elsif @drm_mornings.update_attributes(morning_params)
redirect_to 'ep_feelings'
else
flash[:error] = "something is broken"
render 'drm/intro'
end
end
我要如何满足两个条件,其中之一是为一个空值的最近的单个记录进行数据库检索属性?
目的是为当前用户的最近Drm获取第一个不完整DrmMorning记录。这些记录在创建后分两个阶段进行编辑,因此 - 如果这是nil
,则该作业的结构为nil
。
每当我打电话的方法它的行为就像@drm_morning
是空的,发送到drm/intro
与闪光灯说感谢完成。
虽然这个记录不应该是空的。在当我打电话控制台DrmMorning.last
我得到这样的输出:
DrmMorning id: 8, name: "breakfast", begain: "7:00", end: "7:30", drm_id: 3, user_id: 1, created_at: "2013-10-12 20:04:29", updated_at: "2013-10-12 20:04:29", commuting: nil, shopping: nil, housework: nil, other_action_words: nil, other_action: nil, location: nil, interaction: nil, spouse: nil, friend: nil, co_worker: nil, parent: nil, child: nil, client: nil, boss: nil, student: nil, other_person: nil, other_person_words: nil, happy: nil, depressed: nil, angery: nil, enjoy: nil, pain: nil, fatigue: nil, active: nil, person_id: nil
当我读到这一点,应满足在更新操作的条件。我检查并且current_user
函数正在工作,并且我已经能够将@drm
变量与其他地方的代码一起分配。我使用的地方不正确?
用于检查零值的语法是“ATTRIBUTE IS NULL”而不是“ATTRIBUTE =零”,这会在该记录的值“零”。
如果你已经解决了这个,然后添加一个答案检查等等它离开了未答复的队列。欢迎来到s/o :) – dax
谢谢 - 它不会让我回答,直到上午,因为我没有足够的代表。在明天之前,我不会选择我自己的答案,但我会暂时选择埃德加的,以便将其排除在队列之外。 – user2084042