# HELP with self-referential join (using another model as join table)

**URL:** <https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143572>\
**Category:** Rails\
**Created:** [July 20, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143572 "2008-07-20T18:47:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sazima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/43a26b/32.png) [@Sazima](https://www.ruby-forum.com/u/Sazima)\
**Post date:** [July 20, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/help-with-self-referential-join-using-another-model-as-join-table/143572/1 "2008-07-20T18:47:36Z")

</div>

Hi all,

I’m having problems with the following relationship, can anybody help  
me out here? I’m using Rails 2.1. There is only i vote in the DB (and  
the 2 users exist):

id voter\_id voted\_id type value status created\_at  
updated\_at  
2 15 17 0 APPROVED 0 2008-07-20 16:18:06 2008-07-20  
16:18:06

Cheers, Sazima

# Table name: votes

# 

# Field Type Null Default

# id int(11) No auto\_increment

# voter\_id int(11) No

# voted\_id int(11) No

# type int(11) No 0

# value varchar(255) Yes NULL

# status int(11) Yes 0

# created\_at datetime Yes NULL

# updated\_at datetime Yes NULL

# 

class Vote \< ActiveRecord::Base

belongs\_to :voter, :class\_name =\> :user, :foreign\_key =\> :voter\_id  
belongs\_to :voted, :class\_name =\> :user, :foreign\_key =\> :voted\_id

end

# 

* * *

# Table name: users

# 

# id :integer(11) not null, primary key

# login :string(255)

# crypted\_password :string(40)

# salt :string(40)

# created\_at :datetime

# updated\_at :datetime

class User \< ActiveRecord::Base

# Votes

has\_many :votes\_made, :class\_name =\> :vote, :foreign\_key  
=\> :voter\_id  
has\_many :votes\_received, :class\_name =\> :vote, :foreign\_key  
=\> :voted\_id  
has\_many :votees, :through =\> :votes\_made, :source =\> :user, :uniq  
=\> true  
has\_many :voters, :through =\> :votes\_received, :source  
=\> :user, :uniq =\> true

end

* * *

THE ERROR I GET IS:

SyntaxError in VotesController#index

compile error  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:1907: syntax error, unexpected tINTEGER  
Object::0  
^

RAILS\_ROOT: C:/Personal/INSTAN~2/rails\_apps/dateclub  
Application Trace | Framework Trace | Full Trace

C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:1907:in `compute_type' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:1428:in`instantiate’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:582:in `find_by_sql' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:582:in`collect!’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:582:in `find_by_sql' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:1341:in`find\_every’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:536:in `find_without_defaults' vendor/plugins/less_monkey_patching/lib/active_record_base.rb:13:in`find’  
app/controllers/votes\_controller.rb:46:in `index’

C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:1907:in `compute_type' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:1428:in`instantiate’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:582:in `find_by_sql' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:582:in`collect!’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:582:in `find_by_sql' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:1341:in`find\_every’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:536:in `find_without_defaults' vendor/plugins/less_monkey_patching/lib/active_record_base.rb:13:in`find’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/base.rb:1162:in `send' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/base.rb:1162:in`perform\_action\_without\_filters’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/filters.rb:580:in `call_filters' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/filters.rb:573:in`perform\_action\_without\_benchmark’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' C:/Personal/INSTAN~2/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/rescue.rb:201:in`perform\_action\_without\_caching’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/caching/sql\_cache.rb:13:in `perform_action' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/connection_adapters/abstract/query_cache.rb:33:in`cache’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/query\_cache.rb:8:in `cache' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/caching/sql_cache.rb:12:in`perform\_action’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/base.rb:529:in `send' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/base.rb:529:in`process\_without\_filters’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/filters.rb:569:in  
`process_without_session_management_support' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/session_management.rb:130:in`process’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/base.rb:389:in `process' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/dispatcher.rb:149:in`handle\_request’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/dispatcher.rb:107:in `dispatch' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/dispatcher.rb:104:in`synchronize’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/dispatcher.rb:104:in `dispatch' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/dispatcher.rb:120:in`dispatch\_cgi’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/dispatcher.rb:35:in `dispatch' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel/rails.rb:76:in`process’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel/rails.rb:74:in `synchronize' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel/rails.rb:74:in`process’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:159:in `process_client' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:158:in`each’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:158:in `process_client' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:285:in`run’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:285:in `initialize' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:285:in`new’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:285:in `run' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:268:in`initialize’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:268:in `new' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:268:in`run’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel/configurator.rb:282:in `run' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel/configurator.rb:281:in`each’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel/configurator.rb:281:in `run' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/bin/mongrel_rails:128:in`run’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel/command.rb:212:in `run' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/bin/mongrel_rails:281 C:/Personal/INSTAN~2/ruby/bin/mongrel_rails:19:in`load’  
C:/Personal/INSTAN~2/ruby/bin/mongrel\_rails:19

C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:1907:in `compute_type' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:1428:in`instantiate’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:582:in `find_by_sql' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:582:in`collect!’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:582:in `find_by_sql' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/base.rb:1341:in`find\_every’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/base.rb:536:in `find_without_defaults' vendor/plugins/less_monkey_patching/lib/active_record_base.rb:13:in`find’  
app/controllers/votes\_controller.rb:46:in `index' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/base.rb:1162:in`send’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/base.rb:1162:in `perform_action_without_filters' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/filters.rb:580:in`call\_filters’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/filters.rb:573:in `perform_action_without_benchmark' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/benchmarking.rb:68:in`perform\_action\_without\_rescue’  
C:/Personal/INSTAN~2/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/benchmarking.rb:68:in`perform\_action\_without\_rescue’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/rescue.rb:201:in `perform_action_without_caching' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/caching/sql_cache.rb:13:in`perform\_action’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/  
lib/active\_record/connection\_adapters/abstract/query\_cache.rb:33:in  
`cache' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/ lib/active_record/query_cache.rb:8:in`cache’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/caching/sql\_cache.rb:12:in `perform_action' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/base.rb:529:in`send’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/base.rb:529:in `process_without_filters' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/filters.rb:569:in`process\_without\_session\_management\_support’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/session\_management.rb:130:in `process' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/base.rb:389:in`process’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/dispatcher.rb:149:in `handle_request' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/dispatcher.rb:107:in`dispatch’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/dispatcher.rb:104:in `synchronize' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/dispatcher.rb:104:in`dispatch’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/  
action\_controller/dispatcher.rb:120:in `dispatch_cgi' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/ action_controller/dispatcher.rb:35:in`dispatch’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel/rails.rb:76:in `process' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel/rails.rb:74:in`synchronize’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel/rails.rb:74:in `process' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:159:in`process\_client’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:158:in `each' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:158:in`process\_client’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:285:in `run' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:285:in`initialize’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:285:in `new' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:285:in`run’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:268:in `initialize' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel.rb:268:in`new’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel.rb:268:in `run' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel/configurator.rb:282:in`run’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/lib/mongrel/configurator.rb:281:in `each' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel/configurator.rb:281:in`run’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/bin/mongrel\_rails:128:in `run' C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86- mswin32-60/lib/mongrel/command.rb:212:in`run’  
C:/Personal/INSTAN~2/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.4-x86-  
mswin32-60/bin/mongrel\_rails:281  
C:/Personal/INSTAN~2/ruby/bin/mongrel\_rails:19:in `load’  
C:/Personal/INSTAN~2/ruby/bin/mongrel\_rails:19

Request

Parameters:

None

Show session dump

* * *

:user: 15  
:return\_to: /  
flash: !map:ActionController::Flash::FlashHash {}
