ruby on rails - Is there any method for distinguishing a record of an association? -
i'm looking method, rails, distinguishing 1 record (ex: @user) of association(ex: @users).
here i'm trying :
def my_method(var) if var == #record # elsif var == #association #do else end end
var
can record or association @user, @users, @page, @pages, ...
i tried solve .class or .each method without success ...
a record instance of activerecord
base, association enumerable collection.
you can explicitly check type
def my_method(var) if var.is_a? activerecord::base # single instance else # collection, association end end
or rely on ruby duck-tying.
def my_method(var) if var.respond_to? :each # collection, association else # single instance end end
duck-typing preferred approach.
ruby-on-rails class activerecord ruby-on-rails-4 comparison
No comments:
Post a Comment