# Method from string?

**URL:** <https://www.ruby-forum.com/t/method-from-string/128700>\
**Category:** Rails\
**Created:** [February 10, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/method-from-string/128700 "2008-02-10T18:55:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [February 10, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/method-from-string/128700/1 "2008-02-10T18:55:02Z")

</div>

I am sure this is doable but I can’t find a specific example and my  
brain is frazzled.  
Basically, I am doing a whole load of in-place editing and need  
methods to support the updates.

Here is what I am doing now:

def set\_blog\_website  
faq = Faq.find(params[:id])  
faq.blog\_website = params[:value]  
faq.save!  
@faq = faq  
do\_ret  
end

Surely there is a generic way I can do this so that I can do something  
like  
def set\_blog\_website  
@faq = update\_column(:blog\_website)  
end

def update\_column(column\_name)  
faq = Faq.find(params[:id])  
faq.column\_name = params[:value]\<\<-- here’s what I don’t know how to  
do!  
faq.save!  
@faq = faq  
do\_ret  
end

Any ideas on how I could set the attribute? Faq is an  
ActiveRecord::Base object.

Thanks

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [February 10, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/method-from-string/128700/2 "2008-02-10T19:15:09Z")

</div>

On 10 Feb 2008, at 17:54, phil wrote:

> I am sure this is doable but I can’t find a specific example and my  
> brain is frazzled.  
> Basically, I am doing a whole load of in-place editing and need  
> methods to support the updates.

Take a look at the update\_attribute method

Fred

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [February 10, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/method-from-string/128700/3 "2008-02-10T19:19:16Z")

</div>

On Feb 10, 9:54 am, phil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> faq.save!
> 
> ```
> 
> def update\_column(column\_name)
> 
> Thanks

I think this will do what you want:

faq[column\_name] = params[:value]

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [February 11, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/method-from-string/128700/4 "2008-02-11T14:29:24Z")

</div>

Thanks guys - I found this does the trick:

def set\_column(column\_name)  
faq = Faq.find(params[:id])  
faq.update\_attribute(column\_name, params[:value])  
@faq = faq  
do\_ret  
end

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [February 11, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/method-from-string/128700/5 "2008-02-11T22:29:59Z")

</div>

Why aren’t you just defining it as @faq at the start? That would get rid  
of  
the needeless definition of the local variable “faq” and the line where  
you  
tell it the instance is the same as the local.

What is do\_ret?

On Feb 11, 2008 11:58 PM, phil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Here is what I am doing now:  
> > > like  
> > > do\_ret

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
