Saturday, 15 June 2013

ruby on rails - Get the model an Active Record object is in -



ruby on rails - Get the model an Active Record object is in -

i'm making activerecord query joins contents of each of models array called @contents. need display content different info comes specific model.

so in view need perform sort of test on elements of array:

<% @contents.each |c| %> <article> <% if [test event] %> event html <% elsif [test post] %> post html <% end %> </article> <% end %>

how model c comes from?

this can trick:

c.kind_of?(event) #=> true or false going deeper: ruby: kind_of? vs. instance_of? vs. is_a?

my short version of comparison:

3.class #=> fixnum 3.is_a? integer #=> true 3.kind_of? integer #=> true 3.instance_of? integer #=> false # is_a? & kind_of? can 'detect' subclasses, when instance_of? not

ruby-on-rails ruby rails-activerecord

No comments:

Post a Comment