Single Table Inheritance and .class problem

Hi all,

I'm trying to do a condition in my application.html.erb where if a user
is based off of a STI it has a "special" section in the navigation.

The following code correctly prints out the user type (either User or
Client)

[code]

<%= current_user.class if logged_in? %>

[/code]

However, when I try to do an if search (example below) it work at all.

[code]

<%= content_tag(:li, link_to ("Products", productss_path)) if
current_user.class == "Client" %>

[/code]

If I change "Client" in the above code to "User" it still doesn't work.
Is there some special case or usage required to handle
current_user.class in an if loop?

Thanks in advance!
-Tony

Tony Tony wrote:

<%= content_tag(:li, link_to ("Products", productss_path)) if
current_user.class == "Client" %>

Oops - typo in the above code: products_path not productss_path

Any help is appreciated!

".class" doesn't return a string, it returns a class...

<%= content_tag(:li, link_to ("Products", productss_path)) if
current_user.class == Client %>

but you might be better using "is_a?(base_class)" - depends what the
intention of your code is:

<%= content_tag(:li, link_to ("Products", productss_path)) if
current_user.is_a?(Client) %>

Michael Pavling wrote:

".class" doesn't return a string, it returns a class...

<%= content_tag(:li, link_to ("Products", productss_path)) if
current_user.class == Client %>

but you might be better using "is_a?(base_class)" - depends what the
intention of your code is:

<%= content_tag(:li, link_to ("Products", productss_path)) if
current_user.is_a?(Client) %>

THANK YOU MICHAEL!

I get the hard stuff working but simple stuff like this gets me stuck
often. Besides the craziness of the Rails API (although it IS helpful),
if there a place where I can learn this kind of thing simple thing?

Thanks again!

-Tony

'Fraid you've just gotta read the docs and remember as much as you can
- and be suspicious! If something doesn't "work" then make sure all
your assumptions are correct - we normally assume things like return
values; object types; variables not being nil; existence of methods...
all that stuff :slight_smile:

I can't recommend enough this search in Google:

"ruby rails api <your problem>"

I had an inkling about the solution to your problem and Googled for
"ruby api object", that took me to the docs for the .class method, and
the answer was right there.

Michael Pavling wrote:

is there a place where I can learn this kind of thing simple thing?

'Fraid you've just gotta read the docs and remember as much as you can
- and be suspicious! If something doesn't "work" then make sure all
your assumptions are correct - we normally assume things like return
values; object types; variables not being nil; existence of methods...
all that stuff :slight_smile:

I can't recommend enough this search in Google:

"ruby rails api <your problem>"

I had an inkling about the solution to your problem and Googled for
"ruby api object", that took me to the docs for the .class method, and
the answer was right there.

Thank you!

I was able to find the .class declaration via your search term. I guess
I just didn't know that the "current_user" part was an object. I'm
telling you - simple stuff.

Luckily I've gotten far from using basic knowledge and hacking/stitching
things together until they work. I hate it though - I'd love to know my
programming was solid. I'm going to dedicate a few weeks to learning the
basics to avoid more embarrassing situations like these. :slight_smile:

-Tony

In Ruby, *everything* is an object :slight_smile:

In ruby all objects are Objects.

Colin

Not sure if the best part was identical answers or identical answer
times!

This forum kicks butt for putting up and helping us newbies out. Thank
you guys.

-Tony

Michael, we will have to stop meeting like this, people will begin to talk.

Actually I baulked at saying everything is an object, in some
languages even the operators are objects and I think that is not the
case in ruby.

Colin