2

I am trying to do a record search with a where method in which there are two conditions, one of which must be nil.

I'm using Rails 4, Ruby 2.0.0, and Postgres.

I have an app with three models: User, Drm, and DrmMorning. Users have many Drms, and Drms have many DrmMornings. I am trying to isolate a single DrmMorning record so that I can edit it.

This is the update action in my drm_mornings controller:

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

How do I perform a database search for the most recent single record that satisfies two conditions, one of which is the null-value for one of the attributes?

The intent is to grab the first incomplete DrmMorning record for the most recent Drm of the current user. These records are edited in two stages after creation, hence the - if this is nil, if that is nil structure of the assignment.

Whenever I call the method it acts like @drm_morning is empty, sending to drm/intro with a flash saying thanks for finishing up.

That record shouldn't be empty, though. In the console when I call DrmMorning.last I get this output:

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

As I read this it should satisfy the condition in the update action. I checked and the current_user function is working, and I've been able to assign the @drm variable with just that code elsewhere. Am I using where incorrectly?


The syntax for checking nil values is "ATTRIBUTE IS NULL" rather than "ATTRIBUTE = nil", which looks for the value 'nil' in the record.

4

2 回答 2

5

The syntax within the where function for checking nil values is not

ATTRIBUTE = nil

but rather

ATTRIBUTE IS nil
于 2013-10-13T13:55:48.827 回答
0

Try putting a breakpoint and play in the console, something obviously is different from what You are searching in console. Gem for debugging https://github.com/deivid-rodriguez/byebug

Also You have a strange table design - why You need a user column in DrmMorning if DrmMorning belongs to Drm and Drm belongs to user? You can access those from users using has_many through.

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

于 2013-10-12T21:05:18.493 回答