Controller / irb

28 views
Skip to first unread message

Mohammed Rashid

unread,
Sep 24, 2014, 7:13:20 AM9/24/14
to rubyonra...@googlegroups.com
This is my controller:

class Members < ActiveRecord::Base
def name
return name + ""
end

def describe
return name + "" + email + "" + mobile + "" + category + "" + other
end
end


when i go into the rails console and connect to Members, I say:

a = Members.first

a.name <<< this should give me the "name" of that person but it does
not, it says:

SystemStackError: stack level too deep

--
Posted via http://www.ruby-forum.com/.

Muskalek

unread,
Sep 24, 2014, 7:32:06 AM9/24/14
to rubyonra...@googlegroups.com
That is because your name method is called recursively - the `return
name + ""` is calling `name` once again and again and again... If you
want to access raw attributes from database on ActiveRecord models, you
can always use `self[:name]`.

It might be convenient for you to use ruby interpolations in a method
like `describe`. The following is equivalent to your method (you don't
need return either - the last thing evaluated is always returned from a
method):

def describe
"#{name}#{email}#{mobile}#{category}#{other}"
end

I don't understand the idea behind `+ ""`. If you want convert any value
to string, it is convention in ruby to call #to_s method on it. If you
use anything in an interpolation, ruby will call #to_s on this object
for you.

Michał.

Mohammed Rashid

unread,
Sep 24, 2014, 1:39:21 PM9/24/14
to rubyonra...@googlegroups.com
Muskalek wrote in post #1158351:
That is because your name method is called recursively - the `return
name + ""` is calling `name` once again and again and again... If you
want to access raw attributes from database on ActiveRecord models, you
can always use `self[:name]`.

It might be convenient for you to use ruby interpolations in a method
like `describe`. The following is equivalent to your method (you don't
need return either - the last thing evaluated is always returned from a
method):

> def describe
> "#{name}#{email}#{mobile}#{category}#{other}"
> end
>
I don't understand the idea behind `+ ""`. If you want convert any
value
to string, it is convention in ruby to call #to_s method on it. If you
use anything in an interpolation, ruby will call #to_s on this object
for you.

Michał.

Thank you very much for your help sir :)

Mohammed Rashid

unread,
Sep 26, 2014, 8:56:36 AM9/26/14
to rubyonra...@googlegroups.com
Thank you Michal, your post is at must help, however I have discovered a
new gem called fullcalendar. Do you think that with this, you can link
these id's so that we can view each attribute in a calendar. So
basically like the day will be on the left but then I will be able to
display this persons "#{name}#{email}#{mobile}#{category}#{other}"
also on the calendar.?
Reply all
Reply to author
Forward
0 new messages