# Init attributes and set value

**URL:** <https://www.ruby-forum.com/t/init-attributes-and-set-value/151835>\
**Category:** Ruby\
**Created:** [October 26, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/init-attributes-and-set-value/151835 "2008-10-26T16:54:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [October 26, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/init-attributes-and-set-value/151835/1 "2008-10-26T16:54:39Z")

</div>

Hey all,

sorry for the noob question.

But how can i init a big amount of attributes with empty strings.  
And overwrite the empty string if the field of the form is filled with  
some value.

I try update\_attributes(hash) with no success.

Thanks for your help! 🙂

---

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [October 26, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/init-attributes-and-set-value/151835/2 "2008-10-26T17:10:46Z")

</div>

On 26.10.2008 16:54, Stephan M. wrote:

> But how can i init a big amount of attributes with empty strings.

I’d use nil as the marker for uninitialized. This happens to be the  
default and saves the overhead of all the empty strings. You can deal  
with those nils later, i.e. when changing or outputting them, e.g.

def modify\_whatever(s)  
(@whatever ||= “”) \<\< s  
end

def print\_whatever  
print @whatever || “”  
end

You can even automate this by writing your own attr\_accessor method that  
will define getters like

def foo  
@foo || “”  
end

> And overwrite the empty string if the field of the form is filled with  
> some value.

Form, what form?

> I try update\_attributes(hash) with no success.

Can you be more specific about what you are really trying to do? Is  
this web, UI or what?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [October 26, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/init-attributes-and-set-value/151835/3 "2008-10-26T17:16:27Z")

</div>

Hey,

thanks for your answer.

I have a form (html rails form) and want to generate a search url with  
every textfield.

The URL looks like [Example Domain](http://example.com?value1=&value2=&value3=&) …

The url has about 20 values but its not necessary every value has to be  
filled.  
So i want to update only these values from my “search form” wich have a  
value and the rest with an empty string. 🙂

thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![pure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pure](https://www.ruby-forum.com/u/pure)\
**Post date:** [October 26, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/init-attributes-and-set-value/151835/4 "2008-10-26T17:31:18Z")

</div>

On Sun, Oct 26, 2008 at 12:16 PM, Stephan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> filled.  
> So i want to update only these values from my “search form” wich have a  
> value and the rest with an empty string. 🙂
> 
> thanks for your help 🙂

I’m sure Robert helped answer your question and I know this isn’t the  
Rails list, but this will hopefully help even further.

> **[\#111 Advanced Search Form - RailsCasts](http://railscasts.com/episodes/111-advanced-search-form)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

HTH,  
Michael G.
