Kelvin Tan
Kelvin Tan

Reputation: 992

Active record find_by_sql, object field return nil in the first time

I run the following code in rails console:

functions = Function.find_by_sql("Select * from functions where active = 1 and role_id =1")

It returns:

[#<Function id: 5355, display: "New User", module_name: "Users Management">]

When I get the display field:

functions[0].display

It returns nil value:

#<Function:0x7f714011e870>=> nil

get the module names:

functions[0].module_name

outputs:

"/users/new"

get the display field again:

 functions[0].display

Now it is correct

 "New User"

This is function.rb

# == Schema Information
# Schema version: 20120327101526
#
# Table name: functions
#
#  id              :integer(4)      not null, primary key
#  display         :string(255)
#  module_name     :string(255)

class Function < ActiveRecord::Base
  belongs_to :role
end

Can somebody explain to me why functions[0].display returns nil in the first time query?

Upvotes: 0

Views: 411

Answers (0)

Related Questions