More advanced Ruby on Rails

Q

Quintin Paulson

------_=_NextPart_001_01C50495.304530EA
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable


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.. :)=20

Thank you

-Quintin

------_=_NextPart_001_01C50495.304530EA--
 
J

John Wilger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top