# Where is the attribute definition of Category class?

**URL:** <https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302>\
**Category:** Ruby\
**Created:** [December 25, 2005, 5:10am UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302 "2005-12-25T05:10:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2005, 5:10am UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/1 "2005-12-25T05:10:53Z")

</div>

Hi everyone, I’m a newbie of ruby and rubyonrails.  
I’m trying \<\> those days, and I found some code generated by  
rails (precisely ActiveRecord) hard to understand, as follows:

class Category \< ActiveRecord::Base  
validates\_length\_of :category, :within =\> 1…20  
validates\_uniqueness\_of :category, :message =\> “already exists”  
end

I know this code define a class according to the Catetory table in db,  
but I cannot find the code defining attributes corresponing to the  
fields in Category table.

Can anybody help me?  
Thanks in advance and merry christmas

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2005, 6:13am UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/2 "2005-12-25T06:13:56Z")

</div>

On Dec 24, 2005, at 11:07 PM, removed\_email\_address@domain.invalid wrote:

> I know this code define a class according to the Catetory table in db,  
> but I cannot find the code defining attributes corresponing to the  
> fields in Category table.
> 
> Can anybody help me?  
> Thanks in advance and merry christmas

In most cases, you won’t find any code in Active Record objects that  
define table attributes because, through the power of Ruby, it can be  
inferred at runtime.

> **[Radar - O’Reilly](https://www.oreilly.com/radar/?page=3)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

FYI - In the future, it might be better to direct your Rails  
questions to the Rails mailing list instead of this one.

[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

~ ryan ~

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2005, 12:45pm UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/3 "2005-12-25T12:45:15Z")

</div>

Hi –

On Sun, 25 Dec 2005, removed\_email\_address@domain.invalid wrote:

> but I cannot find the code defining attributes corresponing to the  
> fields in Category table.
> 
> Can anybody help me?

It’s done at runtime via method\_missing. Have a look in  
lib/active\_record/base.rb, in the ActiveRecord source. For version  
1.13.2 you’ll find method\_missing definition on line 1479.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2005, 3:47pm UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/4 "2005-12-25T15:47:28Z")

</div>

On Dec 25, 2005, at 8:12 AM, removed\_email\_address@domain.invalid wrote:

> 

Obviously every organization’s coding practices differ, but I’ll bet  
that your colleagues would rather have good documentation for your  
classes rather than having your classes perfectly insulated in their  
own little world, regardless if your using Rails or not.

Also, the Rails philosophy is more about using conventions over  
configuration. The way Active Record handles table attributes is a  
convention that, once internalized, allows significant speed up in  
development time. If everyone in your organization understands this  
convention, there’s no need to go against that grain.

Again, I encourage you to talk directly with Rails community as  
they’re probably better suited to answer these sorts of questions.

~ ryan ~

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 25, 2005, 2:15pm UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/5 "2005-12-25T14:15:29Z")

</div>

I know, i know,🙂  
ActiveRecord that by implementing the Object#method\_missing method  
which will be called whevever trying to access a attribute not defined  
yet.  
Is that right?  
But in a big team-developing software, how can I enforce the precise  
reference of my classes’ attributes by other developers, not refering  
to non-exist attribute? of course this will raise “column not found”  
exception at runtime, but can we prevent it from the very beginning.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 26, 2005, 12:49am UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/6 "2005-12-26T00:49:55Z")

</div>

Thanks, ryan. I’v joined the rails mailing list now, but I think as we  
going  
on, this problem is  
becoming more like a ruby problem,~!~  
I think if there is any compile tool in ruby that is powerful enough to  
check the reference to my class  
and the database structure to determine if the references are valid,  
or if there is any unit test automatation tool which can generate cases  
for  
that purpose.

Code convertion is a way, but only suitable for experienced programmer.  
And the defer of the attribute definition will cause some word-complete  
function not work, such as  
that in source insight.

And is it dangerous if I supply a library that any user can fill any  
attributes into it by using method\_missing? A library must be able to  
deal  
with any misuage or malicious attack. Safety guaranteed by documentation  
is  
not really safe.

2005/12/25, J. Ryan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 26, 2005, 5:38am UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/7 "2005-12-26T05:38:19Z")

</div>

Rails implements access to record fields using at runtime using  
method\_missing. If there is no such field in the database which maps to  
the attribute called on the Active Record then an exception is  
thrown.Simple! I dont see how this can be construed as a security  
problem.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 26, 2005, 5:11am UTC](https://www.ruby-forum.com/t/where-is-the-attribute-definition-of-category-class/54302/8 "2005-12-26T05:11:17Z")

</div>

On Dec 25, 2005, at 6:49 PM, Sadys HumbleBee wrote:

> Thanks, ryan. I’v joined the rails mailing list now, …

Good work! 😃

> but I think as we going on, this problem is becoming more like a  
> ruby problem,~!~

Nuts.

> I think if there is any compile tool in ruby that is powerful  
> enough to check the reference to my class and the database  
> structure to determine if the references are valid,

We are still talking about Rails, right? ActiveRecord determines the  
columns in a database table dynamically at runtime. In other words,  
Rails doesn’t check the ‘references’ to the table, it _creates_  
them. Here an example SQL table definition and an associated  
ActiveRecord sub-class definition.

create table friends (  
id int not null auto\_increment,  
name varchar(100) not null  
);

class Friend \< ActiveRecord::Base  
end

Now, by running the code

friend = Friend.new  
friend.name = ‘Foo’

Rails automatically adds the following method the Friend class

def name=(value)  
@name = value  
end

and then sends a message to that method with the ‘Foo’ argument.  
This all happens because Friend is a sub-class of ActiveRecord (and  
because you told your Rails app how to find the friend table via the  
databases.yml file too).

> or if there is any unit test automatation tool which can generate  
> cases for that purpose.

Unit testing is an important part of Rails apps, but I’d suggest  
diving right into Rails development before you think about testing.

> Code convertion is a way, but only suitable for experienced  
> programmer. And the defer of the attribute definition will cause  
> some word-complete function not work, such as that in source insight.

I’m not really sure what you meant by these statements.

> And is it dangerous if I supply a library that any user can fill  
> any attributes into it by using method\_missing? A library must be  
> able to deal with any misuage or malicious attack. Safety  
> guaranteed by documentation is not really safe.

ActiveRecord, like most classes, _does_ in fact deal with sending  
messages to non-existent methods: It throws an exception. 🙂

I’m not entirely sure what you’re driving at, but if you haven’t done  
any Rails programming I’d suggest you start with a few tutorials  
online. If you’re still intrigued by Rails and the Ruby language,  
there are some great books over at http://  
[www.pragmaticprogrammers.com](http://www.pragmaticprogrammers.com) that I personally recommend.

Good luck!

~ ryan ~
