Ruby on Rails 5.2 find by vname returns nil for valid value

Boltgreywing

Senior Member
So I am having a bit of an issue. I have a user_id that is filled with the string bbones.

Yet when I try to search for it using User.find_by_vname it results in a nil value being returned.

What weird about this is that the vname actually does exist in the sql table itself.

In Rails 3.2 this worked fine and I had no issues, but with rails 5.2 it doesn't seem to work.

I could really use some help with this issue as I have no idea why this is occurring.

This is a snippet of the code from my colorscheme helper

def displayColorOwner
         value = "Colorscheme List"
         somevalue = params[:user_id]
         if(somevalue)
            userFound = User.find_by_vname(somevalue)
            if(userFound)
               raise "I am found"
               value = (userFound.vname + "'s colorschemes")
            else
               raise "I am not found even though: #{somevalue}"
            end
         end
         return value
      end

 
The syntax has changed, you need to use

User.find_by(vname: some_value)


https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
@Matthew: I discovered what the problem is. Turns out it was a validation error involving this statement.

belongs_to :user

In previous rails build such as rails 3 and 4 this worked.

However in rails 5 it turns out that they added a required field to this.

So to get to work like the old way you need to do belong_to :user, optional: true

This creates problems with populate as it doesn't always jump to users first.

The syntax change wasn't the issue though. While I find the syntax okay I prefer to stick User.find_by_vname as opposed to User.find_by(vname:) I feel the first one is easier to identify what you are talking about then the other one is.

Hopefully my discovery helps out some other users.

 
Back
Top