Sazima
2008-Jul-20 16:41 UTC
HELP with self-referential join (using another model as join table)
Hi all, I''m having problems with the following relationship, can anybody help me out here? I''m using Rails 2.1. 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sazima
2008-Jul-20 16:46 UTC
HELP with self-referential join (using another model as join table)
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 {} --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-20 18:42 UTC
Re: HELP with self-referential join (using another model as join table)
On 20 Jul 2008, at 17:41, Sazima wrote:> > Hi all, > > I''m having problems with the following relationship, can anybody help > me out here? I''m using Rails 2.1. >You don''t say exactly what problems you''re having, so just some quick comments.> 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 >Can''t remember off the top of my head whether it really matters but that should have :class_name => ''User''. You don''t need the foreign key option here - the default is assocation name + _id Your type column will cause problems: rails uses the type column for single table inheritance, as it is you''ll probably get some horrendous error whenever you try and load an instance of Vote.> # > ------------------------------------------------------------------------------------------------------------ > > # 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_idThat should probably also be :class_name => ''Vote''> has_many :votees, :through => :votes_made, :source => :user, :uniq > => true > has_many :voters, :through => :votes_received, :source > => :user, :uniq => trueYou''re not using the :source option correctly. What you''re telling rails is ''so load the votes_made association, then grab the association named x (the default would be votee). So you source option here should be :voter or :voted Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sazima
2008-Jul-20 21:41 UTC
Re: HELP with self-referential join (using another model as join table)
Hey Fred, Thanks a lot for the comments. I had already tried ''User'' and ''Vote'', as well as commented the "has_many ... through" statements, but didn''t thought of the type conflict. That was it! Thanks! Cheers, Sazima P.S.: That would be a nice addition to the migration mechanism: an alert when a column with such a name is used... On Jul 20, 3:42 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 20 Jul 2008, at 17:41, Sazima wrote: > > > > > Hi all, > > > I''m having problems with the following relationship, can anybody help > > me out here? I''m using Rails 2.1. > > You don''t say exactly what problems you''re having, so just some quick > comments. > > > 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 > > Can''t remember off the top of my head whether it really matters but > that should have :class_name => ''User''. > You don''t need the foreign key option here - the default is assocation > name + _id > > Your type column will cause problems: rails uses the type column for > single table inheritance, as it is you''ll probably get some horrendous > error whenever you try and load an > instance of Vote. > > > > > # > > ------------------------------------------------------------------------------------------------------------ > > > # 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 > > That should probably also be :class_name => ''Vote'' > > > has_many :votees, :through => :votes_made, :source => :user, :uniq > > => true > > has_many :voters, :through => :votes_received, :source > > => :user, :uniq => true > > You''re not using the :source option correctly. What you''re telling > rails is ''so load the votes_made association, then grab the > association named x (the default would be votee). > So you source option here should be :voter or :voted > > Fred--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sazima
2008-Jul-20 22:11 UTC
Re: HELP with self-referential join (using another model as join table)
And yeah, you were also right about the :user x '' User'' and :source things... Cheers, Sazima On Jul 20, 6:41 pm, Sazima <rsaz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey Fred, > > Thanks a lot for the comments. I had already tried ''User'' and ''Vote'', > as well as commented the "has_many ... through" statements, but didn''t > thought of the type conflict. That was it! Thanks! > > Cheers, Sazima > > P.S.: That would be a nice addition to the migration mechanism: an > alert when a column with such a name is used... > > On Jul 20, 3:42 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 20 Jul 2008, at 17:41, Sazima wrote: > > > > Hi all, > > > > I''m having problems with the following relationship, can anybody help > > > me out here? I''m using Rails 2.1. > > > You don''t say exactly what problems you''re having, so just some quick > > comments. > > > > 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 > > > Can''t remember off the top of my head whether it really matters but > > that should have :class_name => ''User''. > > You don''t need the foreign key option here - the default is assocation > > name + _id > > > Your type column will cause problems: rails uses the type column for > > single table inheritance, as it is you''ll probably get some horrendous > > error whenever you try and load an > > instance of Vote. > > > > # > > > ------------------------------------------------------------------------------------------------------------ > > > > # 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 > > > That should probably also be :class_name => ''Vote'' > > > > has_many :votees, :through => :votes_made, :source => :user, :uniq > > > => true > > > has_many :voters, :through => :votes_received, :source > > > => :user, :uniq => true > > > You''re not using the :source option correctly. What you''re telling > > rails is ''so load the votes_made association, then grab the > > association named x (the default would be votee). > > So you source option here should be :voter or :voted > > > Fred > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---