# Class\_eval method scope

**URL:** <https://discuss.rubyonrails.org/t/class-eval-method-scope/59552>\
**Category:** rubyonrails-talk\
**Created:** [June 21, 2011, 10:11am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552 "2011-06-21T10:11:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukesh\_Singh](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Mukesh\_Singh](https://discuss.rubyonrails.org/u/Mukesh_Singh)\
**Post date:** [June 21, 2011, 10:11am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/1 "2011-06-21T10:11:53Z")

</div>

class AssigneesController \< ApplicationController

def assignees

Assignee.class\_eval %Q{

def assignee\_name

self[“#{params[:assignee\_by]}”]

end

}

end

def do\_assignee\_cleaning

Assignee.assignee\_name

redirect\_to assignee\_cleaning\_project\_assignees\_path(params[:project\_id])

end

end

I want to use assignee\_name method in do\_assignee\_cleaning action but i am getting error undefined

method `assignee_name' for class `Assignee’ in do\_assignee\_cleaning

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 21, 2011, 10:38am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/2 "2011-06-21T10:38:43Z")

</div>

In an effort to understand the question (not having used class\_eval) I found the following link that suggests, rather non-intuitively, that you should be using instance\_eval rather than class\_eval.

[http://ilikestuffblog.com/2009/01/09/fun-with-rubys-instance\_eval-and-class\_eval/](http://ilikestuffblog.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/)

Colin

---

<div class="post-metadata">

**Author:** ![Mukesh\_Singh](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Mukesh\_Singh](https://discuss.rubyonrails.org/u/Mukesh_Singh)\
**Post date:** [June 21, 2011, 10:45am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/3 "2011-06-21T10:45:16Z")

</div>

sorry for mistake not Assignee.assignee\_name

Assignee.find(1).assignee\_name

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 21, 2011, 10:50am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/4 "2011-06-21T10:50:09Z")

</div>

> sorry for mistake not Assignee.assignee\_name Assignee.find(1).assignee\_name

Do you mean that you have fixed the problem by that change, or that you had a mistake in the question you asked and with Assignee.find(1).assignee\_name it still fails?

Colin

---

<div class="post-metadata">

**Author:** ![Mukesh\_Singh](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Mukesh\_Singh](https://discuss.rubyonrails.org/u/Mukesh_Singh)\
**Post date:** [June 21, 2011, 10:56am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/5 "2011-06-21T10:56:30Z")

</div>

yes still fails Assignee.find(1).assignee\_name

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 21, 2011, 11:13am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/6 "2011-06-21T11:13:18Z")

</div>

> yes still fails Assignee.find(1).assignee\_name

Please don't top post, it makes it difficult to follow the thread. Insert your reply at appropriate points in previous message. Thanks.

Can you post the full error message and the stack trace please along with the exact code (copy/paste rather than re-type).

Colin

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 21, 2011, 11:22am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/7 "2011-06-21T11:22:02Z")

</div>

> yes still fails Assignee.find(1).assignee\_name

I presume that you \*have\* called assignees before calling do\_assignee\_cleaning.

Colin

---

<div class="post-metadata">

**Author:** ![Chirag](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Chirag](https://discuss.rubyonrails.org/u/Chirag)\
**Post date:** [June 21, 2011, 11:50am UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/8 "2011-06-21T11:50:43Z")

</div>

Curious why you would want to define the method that way, are you overriding the method assignee\_name which is already defined in the Assignee class?

If not, then why not define it directly in the model like this:

def assignee\_name(assigned\_by)

self[“assigned\_by”]

end

Controller method will be

def do\_assignee\_cleaning

Assignee.assignee\_name(params[:assignee\_by])

redirect\_to assignee\_cleaning\_project\_assignees\_path(params[:project\_id])

end

---

<div class="post-metadata">

**Author:** ![Mukesh\_Singh](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Mukesh\_Singh](https://discuss.rubyonrails.org/u/Mukesh_Singh)\
**Post date:** [June 21, 2011, 1:31pm UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/9 "2011-06-21T13:31:19Z")

</div>

Thanks for your reply

I thought class\_eval method would permanently add assignee\_name method to Assignee instance object and i can access from other action too.

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 21, 2011, 1:41pm UTC](https://discuss.rubyonrails.org/t/class-eval-method-scope/59552/10 "2011-06-21T13:41:22Z")

</div>

> Thanks for your reply

Please don't top post, it makes it difficult to follow the thread. Insert your reply at appropriate points in previous message. Thanks.

> I thought class\_eval method would permanently add assignee\_name method to Assignee instance object and i can access from other action too.

I think it will, but only when you execute the class\_eval code. Permanent means of course until you restart the server or reload the Assignee class. Since in development mode classes are reloaded at each action request it will not hold from one request to the next, if that is what you are trying to do. Even in production this is a bad thing to do as you never know when a server might get restarted and loose the data. Not to mention the fact that once in production successive actions are not necessarily even run by the same computer. If you want retain information from one action to the next then put it in the session or in the database, or in a hidden field in the intervening view.

I think Chirag Singhal's suggestion is better though unless there is some reason you cannot use it.

Colin
