[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

More advanced Ruby on Rails

Quintin Paulson

1/27/2005 5:26:00 PM


Hello everyone.. I have been playing with ruby on rails a little but but
all of the documentation I can find seems to be quite basic.

Does anyone know of any documentation that shows how to extend a Model
to allow it to use data from multiple tables?. Or does rails always need
a single Model -> table relationship?..

The reason I am asking is In my DB schema I have multiple tables for
user data ( account, details, data ). Any documentation or links to some
examples would be greatly appreciated.. :)

Thank you

-Quintin
1 Answer

John Wilger

1/28/2005 1:05:00 PM

0

On Fri, 28 Jan 2005 02:25:39 +0900, Quintin Paulson <QPaulson@cucbc.com> wrote:
> Does anyone know of any documentation that shows how to extend a Model
> to allow it to use data from multiple tables?. Or does rails always need
> a single Model -> table relationship?..

I don't know of any specific documentation that deals with this issue,
but I've tackled the same issue in my own projects by simply using
associations and then mapping properties of the associated objects
onto the "master" object by overriding #method_missing on the master.

I.e.:

class Details < ActiveRecord::Base
# some implementation...
end

class Data < ActiveRecord::Base
# some implementation,,,
end

class Account < ActiveRecord::Base
has_one :details, :class_name => 'Details'
has_one :data, :class_name => 'Data'

def method_missing(sym, *args, &block)
begin
super(*args, &block)
rescue NoMethodError
begin
self.details.send(sym, *args, &block)
rescue NoMethodError
self.data.send(sym, *args, &block)
end
end
end
end

Of course, this probably isn't the most elegant way of solving the
problem, but it may get you started on some other ideas for approching
the pattern.

--
Regards,
John Wilger

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland