New to rails, quick (hopefully simple) question

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!

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!

On 8/29/07, telefauna [email protected] 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