[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Where is the attribute definition of Category class?

sadys.humblebee@gmail.com

12/25/2005 4:05:00 AM

Hi everyone, I'm a newbie of ruby and rubyonrails.
I'm trying <<Rails4Day>> 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

7 Answers

J. Ryan Sobol

12/25/2005 5:12:00 AM

0


On Dec 24, 2005, at 11:07 PM, sadys.humblebee@gmail.com wrote:

> Hi everyone, I'm a newbie of ruby and rubyonrails.
> I'm trying <<Rails4Day>> 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

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.

http://www.onlamp.com/pub/a/onlamp/2005/10/13/what_is_rails.h...

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/list...

~ ryan ~


dblack

12/25/2005 11:43:00 AM

0

sadys.humblebee@gmail.com

12/25/2005 1:11:00 PM

0

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.

J. Ryan Sobol

12/25/2005 2:47:00 PM

0


On Dec 25, 2005, at 8:12 AM, sadys.humblebee@gmail.com wrote:

> 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.
>
>


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 ~


sadys.humblebee@gmail.com

12/25/2005 11:50:00 PM

0

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 Sobol <ryansobol@gmail.com>:
>
>
> On Dec 25, 2005, at 8:12 AM, sadys.humblebee@gmail.com wrote:
>
> > 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.
> >
> >
>
>
> 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 ~
>
>


--
Xie Wei, A programmer
sadys.humblebee@gmail.com
13917917201

J. Ryan Sobol

12/26/2005 4:11:00 AM

0


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

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

Good work! :-D

> 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 that I personally recommend.

Good luck!

~ ryan ~


Vivek

12/26/2005 4:37:00 AM

0

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.