Can an active records maps to N tables?


#1

Hi,

Is it possbile that one active record maps to N
tables, actually the relation is 1 to 1, I wonder it
is possible.

also can a ruby object map to N tables, instead of an
active records?

Thanks you very much:)

Saiho


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


#2

On 12/21/05, Saiho Y. removed_email_address@domain.invalid wrote:

Will this work? This is the preferred behavior for ActiveRecord:
(Made-up example)

Kingdom < ActiveRecord::Base

table name is ‘kingdoms’

has_one :king
end

King < ActiveRecord::Base

table name is ‘kings’

belongs_to :kingdom
end

table: kingdoms
id
total_population
national_bird

table: kings
id
kingdom_id
hair_color
favorite_dessert

k = King.find(:first, “hair_color = ‘brown’”, :include => ‘kingdom’)

print k.kingdom.total_population
print k.favorite_dessert

You can also have multiple associations to a single table, from one
class.
That means you could call the table ‘nobles’ instead of ‘kings’, and
say:
Kingdom has_many :nobles
Kingdom has_one :king, :class_name => ‘Noble’


#3

Hi,

Thanks you very much for the infromation, but what you
have described, is still 1 to 1, one active record
associate to one table. what I was thinking is 1 to n,
like

I have two tables: Kingdoms, kings
I have one active records: realm

Realm < ActiveRecord::Base
#using two tables
set_table_names Kingdoms
set_table_name kings
end

Can I do this???

Thanks you very much!!!

Saiho

— Wilson B. removed_email_address@domain.invalid wrote:

active records?
has_one :king
national_bird
print k.kingdom.total_population
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


#4

An external library that wraps the ActiveRecord object can do this for
you.

The DataView project (http://dataview.rubyforge.org/), will allow you to
join n data sources (tables) together in a read-only view of the data.

You can also create a custom class to wrap the two ActiveRecord objects.
http://dataview.rubyforge.org/


#5

Hi,

Thanks you for the infromation, I will have a look on
it, at meantime, Do you know how to “discover” a
table, its name and its column via Ruby-rails?

Thanks you very much!!!

Saiho

— Brian T. removed_email_address@domain.invalid wrote:

http://dataview.rubyforge.org/
ActiveRecord.

Hi,
I have one active records: realm

ActiveRecord:
end
favorite_dessert
That means you could call the table ‘nobles’

removed_email_address@domain.invalid


Brian T.
http://weblog.freeopinion.org


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


#6

Hi,

I was thinking, it is possible to “change” the wrapped
table in runtime with avtice records. Like

warp the table_a

class TableA < activeRecords::base

end

change the warpped table

def changeTable
set_table_name “TableB”
end

If it is possible, is there anyway we can access the
different columns of the tables? we presume that both
table don’t have the same columns (name and type)

Thanks you very much

Saiho


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


#7

Sadly you can’t do that with what Rails provides.

The idea behind the “Active Record” pattern is that an object ‘wraps’
a single row in your database. Any configuration that can’t guarantee
that will be a tough thing to fit into ActiveRecord.
That being said, you can make it do anything you feel like writing the
code for. That’s just not a supported or recommended thing to do with
AR.

Good luck,
–Wilson.


#8

On 12/22/05, Saiho Y. removed_email_address@domain.invalid wrote:

change the warpped table

Saiho
Probably not a good idea by any means, but try this:

def change_table
set_table_name ‘foo’
@columns = nil
end


rick
http://techno-weenie.net


#9

Hi,

I try to use “clear_connection_cache!” of the
ActiveRecord::base, but each time I got the following
error:
undefined method `clear_connection_cache!’ for
ActiveRecord::Base:Class

I try to call the function different way, but
unsuccess…

class genBD < ActiveRecords::base

def self.clearConnectionCache

1

clear_connection_cache!

2

self.clear_connection_cache!

3

genDB.clear_connection_cache!

1

activeRecord::base.clear_connection_cache!
end

end

None of those method calling is successful…

Thanks you very much

Saiho


NoMethodError in Gentest#index

undefined method `clear_connection_cache!’ for
ActiveRecord::Base:Class

RAILS_ROOT: .
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activerecord-1.12.2/lib/active_record/base.rb:942:in
method_missing' #{RAILS_ROOT}/app/models/gen_table_as.rb:18:ingetConnection’
#{RAILS_ROOT}/app/controllers/gentest_controller.rb:15:in
`index’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activerecord-1.12.2/lib/active_record/base.rb:942:in
method_missing' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/base.rb:834:inperform_action_without_filters’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/filters.rb:295:in
perform_action_without_benchmark' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/benchmark.rb:293:in
measure' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/rescue.rb:82:in
perform_action' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/base.rb:365:inprocess_without_session_management_support’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/session_management.rb:116:in
process' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/dispatcher.rb:36:indispatch’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/webrick_server.rb:117:in
handle_dispatch' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/webrick_server.rb:83:inservice’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in
service' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:173:in
start_thread' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:95:in
start' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:92:instart’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:23:in
start' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:82:instart’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/webrick_server.rb:69:in
dispatch' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/commands/server.rb:59 #{RAILS_ROOT}usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.2/lib/active_support/dependencies.rb:213:in
`require’
#{RAILS_ROOT}cript/server:5

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activerecord-1.12.2/lib/active_record/base.rb:942:in
method_missing' #{RAILS_ROOT}/app/models/gen_table_as.rb:18:ingetConnection’
#{RAILS_ROOT}/app/controllers/gentest_controller.rb:15:in
index' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/base.rb:834:inperform_action_without_filters’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/filters.rb:295:in
perform_action_without_benchmark' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/benchmark.rb:293:in
measure' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/rescue.rb:82:in
perform_action' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/base.rb:365:inprocess_without_session_management_support’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/action_controller/session_management.rb:116:in
process' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/dispatcher.rb:36:indispatch’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/webrick_server.rb:117:in
handle_dispatch' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/webrick_server.rb:83:inservice’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in
service' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:173:in
start_thread' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:95:in
start' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:92:instart’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:23:in
start' #{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:82:instart’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/webrick_server.rb:69:in
dispatch' #{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/commands/server.rb:59 #{RAILS_ROOT}usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.2/lib/active_support/dependencies.rb:213:in
`require’
#{RAILS_ROOT}cript/server:5

Request

Parameters: None

Show session dump

flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

If God really exists, I would like to
know what the dinosaurs have done to
deserve their extinction.

Water is unknown to fishes,
until they discover air.

http://www.geocities.com/sayoyo/


Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com


#10

I’m not sure if this is causing the problem or not but classes in
ruby have to begin with an Uppercase letter. so genDB should be GenDB

-Ezra

On Dec 22, 2005, at 12:19 PM, Saiho Y. wrote:

activeRecord::base.clear_connection_cache!

Application Trace | Framework Trace | Full Trace
lib/active_record/base.rb:942:in
#{RAILS_ROOT}usr/local/lib/ruby/1.8/benchmark.rb:293:in
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/
`service’
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:92:in
#{RAILS_ROOT}usr/local/lib/ruby/site_ruby/1.8/rubygems/
#{RAILS_ROOT}/app/models/gen_table_as.rb:18:in
action_controller/benchmarking.rb:69:in
action_controller/base.rb:365:in
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/
#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:95:in
#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

If God really exists, I would like to


Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#11

Hi,

it was just a typing error, when I retype the code,
Maj or not, it doesn’t work…

Saiho

— Ezra Z. removed_email_address@domain.invalid wrote:

class genBD < ActiveRecords::base
end


#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activerecord-1.12.2/

lib/active_record/base.rb:942:in
method_missing' #{RAILS_ROOT}/app/models/gen_table_as.rb:18:ingetConnection’

#{RAILS_ROOT}/app/controllers/gentest_controller.rb:15:in

`index’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activerecord-1.12.2/

lib/active_record/base.rb:942:in
`method_missing’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/base.rb:834:in
`perform_action_without_filters’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/filters.rb:295:in
`perform_action_without_benchmark’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/benchmarking.rb:69:in
`perform_action_without_rescue’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/benchmark.rb:293:in

`measure’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/benchmarking.rb:69:in
`perform_action_without_rescue’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/rescue.rb:82:in
`perform_action’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/base.rb:365:in
`process_without_session_management_support’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/session_management.rb:116:in
`process’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

dispatcher.rb:36:in
`dispatch’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

webrick_server.rb:117:in
`handle_dispatch’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

webrick_server.rb:83:in
`service’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in

`service’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in

`run’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:173:in

`start_thread’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:162:in

`start_thread’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:95:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:92:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:23:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:82:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

webrick_server.rb:69:in
`dispatch’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

commands/server.rb:59

#{RAILS_ROOT}usr/local/lib/ruby/site_ruby/1.8/rubygems/

custom_require.rb:21:in
`require’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.2/

lib/active_support/dependencies.rb:213:in
`require’
#{RAILS_ROOT}cript/server:5

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activerecord-1.12.2/

lib/active_record/base.rb:942:in
method_missing' #{RAILS_ROOT}/app/models/gen_table_as.rb:18:ingetConnection’

#{RAILS_ROOT}/app/controllers/gentest_controller.rb:15:in

`index’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/base.rb:834:in
`perform_action_without_filters’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/filters.rb:295:in
`perform_action_without_benchmark’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/benchmarking.rb:69:in
`perform_action_without_rescue’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/benchmark.rb:293:in

`measure’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/benchmarking.rb:69:in
`perform_action_without_rescue’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/rescue.rb:82:in
`perform_action’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/base.rb:365:in
`process_without_session_management_support’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/actionpack-1.10.2/lib/

action_controller/session_management.rb:116:in
`process’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

dispatcher.rb:36:in
`dispatch’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

webrick_server.rb:117:in
`handle_dispatch’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

webrick_server.rb:83:in
`service’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in

`service’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in

`run’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:173:in

`start_thread’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:162:in

`start_thread’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:95:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:92:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:23:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/1.8/webrick/server.rb:82:in

`start’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

webrick_server.rb:69:in
`dispatch’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/rails-0.14.2/lib/

commands/server.rb:59

#{RAILS_ROOT}usr/local/lib/ruby/site_ruby/1.8/rubygems/

custom_require.rb:21:in
`require’

#{RAILS_ROOT}usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.2/

dsl.yahoo.com
WebMaster


Yahoo! for Good - Make a difference this year.
http://brand.yahoo.com/cybergivingweek2005/