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)


<%= current_user.class if logged_in? %>

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


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

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 T. 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!

On 31 March 2010 15:21, Tony T. [email protected] wrote:


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

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?

“.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 P. 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

Michael P. wrote:

On 31 March 2010 15:36, Tony T. [email protected] 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 "

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

On 31 March 2010 16:38, Tony T. [email protected] wrote:

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.

In Ruby, everything is an object :slight_smile:

On 31 March 2010 16:38, Tony T. [email protected] wrote:

I can’t recommend enough this search in Google:
I just didn’t know that the “current_user” part was an object. I’m
telling you - simple stuff.

In ruby all objects are Objects.

Colin

On 31 March 2010 15:36, Tony T. [email protected] 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 "

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.

On 31 March 2010 16:41, Michael P. [email protected] wrote:

On 31 March 2010 16:38, Tony T. [email protected] wrote:

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.

In Ruby, everything is an object :slight_smile:

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs