# New to rails, quick (hopefully simple) question

**URL:** <https://www.ruby-forum.com/t/new-to-rails-quick-hopefully-simple-question/113328>\
**Category:** Rails\
**Created:** [August 29, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/new-to-rails-quick-hopefully-simple-question/113328 "2007-08-29T21:56:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![telefauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@telefauna](https://www.ruby-forum.com/u/telefauna)\
**Post date:** [August 29, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/new-to-rails-quick-hopefully-simple-question/113328/1 "2007-08-29T21:56:56Z")

</div>

I am brand new to rails and I am attempting to make my first web app.

I have quickly run into my first problem however that I can’t seem to  
get by. It seems like it would be easy to get around (with my  
knowledge of other languages besides ruby), but I can’t seem to get by  
it.

Right now, I have a note model that contains attributes, :title  
(string) :content (text) and :tags (string)

In my models/note.rb file, all I have is this:

class Note \< ActiveRecord::Base  
validates\_presence\_of :title, :content  
end

What I want to have done with my tag field though is a different  
matter. What I want to happen is that if the tag field is left blank,  
a place-holding “@untagged” string would be put into the :tags string.

What I have attempted to do is this in the models/note.rb:

if :tags.nil?  
:tags =\> ‘@untagged’  
end

However, I get a syntax error.

app/models/note.rb:3: syntax error, unexpected ‘=’, expecting kEND  
:tags = ‘@untagged’

How should I go about solving this problem? Should I put this in a  
different part of the application to do the comparison?

Thanks for your help in advance!

---

<div class="post-metadata">

**Author:** ![telefauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@telefauna](https://www.ruby-forum.com/u/telefauna)\
**Post date:** [August 29, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/new-to-rails-quick-hopefully-simple-question/113328/2 "2007-08-29T22:48:19Z")

</div>

> where is that statement? Is that in a method? Just inserting that  
> statement into your model without placing it in a method makes no  
> sense. And the format for variable assignment is:
> 
> my\_variable = ‘some value’
> 
> you normally use the =\> symbol when defining a hash

I’m sorry, I forgot to say that I had the statement within a validate  
method. And I accidentally left in the =\> instead of just the = when  
I was playing around with the syntax, trying to get the thing to  
work.

> you could put this into a before\_save method in your model such as follows:
> 
> def before\_save  
> self.tags= ‘@untagged’ if self.tags.blank?  
> end

That worked really nicely. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![telefauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@telefauna](https://www.ruby-forum.com/u/telefauna)\
**Post date:** [August 29, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/new-to-rails-quick-hopefully-simple-question/113328/3 "2007-08-29T22:26:06Z")

</div>

On 8/29/07, telefauna [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I want to have done with my tag field though is a different  
> matter. What I want to happen is that if the tag field is left blank,  
> a place-holding “@untagged” string would be put into the :tags string.
> 
> What I have attempted to do is this in the models/note.rb:
> 
> if :tags.nil?  
> :tags =\> ‘@untagged’  
> end

where is that statement? Is that in a method? Just inserting that  
statement into your model without placing it in a method makes no  
sense. And the format for variable assignment is:

my\_variable = ‘some value’

you normally use the =\> symbol when defining a hash

> However, I get a syntax error.
> 
> app/models/note.rb:3: syntax error, unexpected ‘=’, expecting kEND  
> :tags = ‘@untagged’
> 
> How should I go about solving this problem? Should I put this in a  
> different part of the application to do the comparison?

you could put this into a before\_save method in your model such as  
follows:

def before\_save  
self.tags= ‘@untagged’ if self.tags.blank?  
end

or you could alter your database schema so that the default value for  
tags is always ‘@untagged’ using the following statement:

alter table \<my\_table\> change tags tags varchar(255) default ‘@untagged’

although the first method is preferred since the second may not  
exactly be database agnostic and might not work in a migration either.

Adam
