B
B
Bogdan2017-07-10 11:40:26
Ruby on Rails
Bogdan, 2017-07-10 11:40:26

Rails ActiveRecord method?

Hello. Do not tell me how to implement it correctly, there is a class method in ActiveRecord.
And how can it be reached if its name is passed in a variable. Thanks

# Поля базы
user.userable_type # 'Institution'
user[ :userable_type ] # 'Institution'

# Мотод класса
user.is_institution? # true
user[ :is_institution? ] # nil

models/user.rb
class User < ApplicationRecord
   def is_institution?
    self.userable_type == 'Institution'
  end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bogdan, 2017-07-10
@bogdan_uman

I answer myself Even better like this

def check( name )
  user.try( name ) || false
end

check(:is_intitution?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question