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
on 2005-12-25 05:10
on 2005-12-25 06:13
On Dec 24, 2005, at 11:07 PM, email@example.com 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. http://www.onlamp.com/pub/a/onlamp/2005/10/13/what... 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 ~ ryan ~
on 2005-12-25 12:45
Hi -- On Sun, 25 Dec 2005, firstname.lastname@example.org 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 email@example.com "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black
on 2005-12-25 14:15
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.
on 2005-12-25 15:47
On Dec 25, 2005, at 8:12 AM, firstname.lastname@example.org 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 ~
on 2005-12-26 00:49
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 <email@example.com>:
on 2005-12-26 05:11
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 ~
on 2005-12-26 05:38
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.