# Validate access to object field

**URL:** <https://www.ruby-forum.com/t/validate-access-to-object-field/145158>\
**Category:** Rails\
**Created:** [August 6, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/validate-access-to-object-field/145158 "2008-08-06T16:03:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/validate-access-to-object-field/145158/1 "2008-08-06T16:03:01Z")

</div>

Suppose I have a Model called House with a field called name

how can I validate the name field, I tried name in the validate method  
but  
it doesn’t work, i tried name, :name, House.name, I don’t know how I can  
validate this field within a validate method, I don’t know how I can  
access it.

class ContractGroup \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :locatel\_content\_groups

protected  
def validate  
#name.gsub(" ", “”) this doesn’t work name is nil

```
errors.add_on_empty(:name) # this work

```

reg = /[a-z]\*/  
unless reg.match(name) #this doesn’t work, name is nil but the match  
function return true !  
errors.add(:name, “has invalid format”)#this doesn’t work  
end  
end

end

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/validate-access-to-object-field/145158/2 "2008-08-06T16:12:23Z")

</div>

Ok, now I manage to access field with self.name

But there is still the regular expression to create :

I need name composed of  
a to z  
A to Z  
dash -  
underscore \_

I tried different combination without success

How can I test this

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/validate-access-to-object-field/145158/3 "2008-08-06T17:32:46Z")

</div>

David N. wrote:

> Ok, now I manage to access field with self.name
> 
> But there is still the regular expression to create :
> 
> I need name composed of  
> a to z  
> A to Z  
> dash -  
> underscore \_

match /[a…z A…Z - \_]_/  
or /[a…z - \_]_/i

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/validate-access-to-object-field/145158/4 "2008-08-06T18:05:22Z")

</div>

On 6 Aug 2008, at 16:32, Justin To wrote:

> > underscore \_
> 
> ## match /[a…z A…Z - \_]_/ or /[a…z - \_]_/i

That doesn’t work, for two reasons:

1. 
  - is a special character in a character class, so it has to be the  
first character in the character class (ie [-…])

2. That aside, that regular expression will match any string, because  
all it says is ‘the string contains 0 or more of those characters’.  
You need to anchor it: \A matches the beginning of a string \z matches  
the end:  
so  
/\AHi\z/ will match the string Hi but not the string Hiatus, whereas /  
Hi/ will match both (there are other anchors with slightly different  
semantics: ^ and $ match the beginning and end of lines, and \Z  
matches the end of the string, excluding any trailing \n

Fred

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 7, 2008, 3:19am UTC](https://www.ruby-forum.com/t/validate-access-to-object-field/145158/5 "2008-08-07T03:19:07Z")

</div>

You’ve posted basically the same question (or line-of-thought) in  
THREE separate threads. Please don’t do this as it’s hard to keep  
track of THREE threads.
