Arwuah _
2006-Mar-20 22:56 UTC
[Rails] AHHHhhhhh... has_and_belongs_to_many, that is going on?
I don''t have a class by the name of "ReviewersScoreCard", yet my many to many configuration looks as follows. And, these errors while running some simple test with ./script/console is giving me a headache: ./script/console Loading development environment.>> r = Reviewer.find_first=> #<Reviewer:0x19d0a48 @attributes={"id"=>"1", "first_name"=>"Big", "last_name"=>"Bird"}>>> r.candidates=> [#<Candidate:0x19c6340 @attributes={"status"=>"", "id"=>"1", "sponsor"=>"", "first_name"=>"Jim", "last_name"=>"Jeffrey", "reviewer_id"=>"1"}>, #<Candidate:0x19c62c8 @attributes={"status"=>"", "id"=>"2", "sponsor"=>"", "first_name"=>"Lam", "jack"=>"johnson", "reviewer_id"=>"1"}>]>> r.reviewers_score_cardsNameError: uninitialized constant ReviewersScoreCard from ...dependencies.rb:195:in `const_missing'' from ...association_proxy.rb:12:in `initialize'' from ...has_and_belongs_to_many_association.rb:5:in `eval'' from ...association_proxy.rb:12:in `initialize'' from ...has_and_belongs_to_many_association.rb:5:in `initialize'' from ...associations.rb:753:in `new'' from ...associations.rb:753:in `reviewers_score_cards'' from ...associations.rb:749:in `reviewers_score_cards'' Here are my models: class Reviewer < ActiveRecord::Base has_many :candidates has_and_belongs_to_many :reviewers_score_cards end class ScoreCard < ActiveRecord::Base has_and_belongs_to_many :reviewers_score_cards, :join_table => "reviewers_score_cards" end class Candidate < ActiveRecord::Base belongs_to :reviewers end What is with the reference to ReviewersScoreCard in the error message? I did not create the model ReviewerScoreCard, but even if I did ReviewersScoreCard is being looked up instead. Shouldn''t the model be ReviewerScoreCard?. I even tried creating a model by that name and received the same error. What am I doing wrong? I have tried all the examples I can find on the web about many-to-many and they all work. Any help would be greatly appreciated. Thanks in advance, _arwuah Just in case. My DDL looks as follows: DROP TABLE IF EXISTS reviewers; CREATE TABLE reviewers ( id int unsigned not null auto_increment, first_name varchar(60) not null, last_name varchar(60) not null, primary key(id) ); DROP TABLE IF EXISTS candidates; CREATE TABLE candidates ( id int unsigned not null auto_increment, reviewer_id int unsigned not null default 0, first_name varchar(60) not null, last_name varchar(60) not null, sponsor varchar(120) not null, status varchar(30) not null, constraint fk_cr_reviewer foreign key (reviewer_id) references reviewer(id), primary key(id) ); -- -- Table structure for table `reviewers_score_cards` -- DROP TABLE IF EXISTS reviewers_score_cards; CREATE TABLE reviewers_score_cards ( reviewer_id int unsigned not null default 0, score_card_id int unsigned not null default 0, constraint fk_rsc_reviewer foreign key (reviewer_id) references reviewer(id), constraint fk_rsc_score_card foreign key (score_card_id) references score_cards(id), primary key(reviewer_id, score_card_id) ); -- -- Table structure for table `score_cards` -- DROP TABLE IF EXISTS score_cards; CREATE TABLE score_cards ( id int unsigned not null auto_increment, appears_calm int unsigned not null default 0, ... primary key(id) ); -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060320/9209dde8/attachment.html
Michael Kaiser
2006-Mar-20 23:12 UTC
[Rails] AHHHhhhhh... has_and_belongs_to_many, that is going on?
Hey Arwuah _ wrote:> I don''t have a class by the name of "ReviewersScoreCard", yet my many to > many configuration looks as follows. And, these errors while running > some simple test with ./script/console is giving me a headache: > > ./script/console > Loading development environment. > >> r = Reviewer.find_first > => #<Reviewer:0x19d0a48 @attributes={"id"=>"1", "first_name"=>"Big", > "last_name"=>"Bird"}> > >> r.candidates > => [#<Candidate:0x19c6340 @attributes={"status"=>"", "id"=>"1", > "sponsor"=>"", "first_name"=>"Jim", "last_name"=>"Jeffrey", > "reviewer_id"=>"1"}>, #<Candidate:0x19c62c8 @attributes={"status"=>"", > "id"=>"2", "sponsor"=>"", "first_name"=>"Lam", "jack"=>"johnson", > "reviewer_id"=>"1"}>] > >> r.reviewers_score_cards > NameError: uninitialized constant ReviewersScoreCard > from ...dependencies.rb:195:in `const_missing'' > from ...association_proxy.rb:12:in `initialize'' > from ...has_and_belongs_to_many_association.rb:5:in `eval'' > from ...association_proxy.rb:12:in `initialize'' > from ...has_and_belongs_to_many_association.rb:5:in `initialize'' > from ...associations.rb:753:in `new'' > from ...associations.rb:753:in `reviewers_score_cards'' > from ...associations.rb:749:in `reviewers_score_cards'' > > > Here are my models: > > class Reviewer < ActiveRecord::Base > has_many :candidates > has_and_belongs_to_many :reviewers_score_cards > end > > class ScoreCard < ActiveRecord::Base > has_and_belongs_to_many :reviewers_score_cards, > :join_table => "reviewers_score_cards" > endThat should be something like: class Reviewer < ActiveRecord::Base has_many :candidates has_and_belongs_to_many :scorecards end class ScoreCard < ActiveRecord::Base has_and_belongs_to_many :reviewers end And if you name your join table reviewers_scorecards (without the second underscore) rails should pick it up automatically. Michael
Rob Biedenharn
2006-Mar-26 21:55 UTC
[Rails] AHHHhhhhh... has_and_belongs_to_many, that is going on?
On Mar 20, 2006, at 6:12 PM, Michael Kaiser wrote:> Hey > > Arwuah _ wrote: >> I don''t have a class by the name of "ReviewersScoreCard", yet my >> many to many configuration looks as follows. And, these errors >> while running some simple test with ./script/console is giving me >> a headache: >> ./script/console >> Loading development environment. >> >> r = Reviewer.find_first >> => #<Reviewer:0x19d0a48 @attributes={"id"=>"1", >> "first_name"=>"Big", "last_name"=>"Bird"}> >> >> r.candidates >> => [#<Candidate:0x19c6340 @attributes={"status"=>"", "id"=>"1", >> "sponsor"=>"", "first_name"=>"Jim", "last_name"=>"Jeffrey", >> "reviewer_id"=>"1"}>, #<Candidate:0x19c62c8 @attributes= >> {"status"=>"", "id"=>"2", "sponsor"=>"", "first_name"=>"Lam", >> "jack"=>"johnson", "reviewer_id"=>"1"}>] >> >> r.reviewers_score_cards >> NameError: uninitialized constant ReviewersScoreCard >> from ...dependencies.rb:195:in `const_missing'' >> from ...association_proxy.rb:12:in `initialize'' >> from ...has_and_belongs_to_many_association.rb:5:in `eval'' >> from ...association_proxy.rb:12:in `initialize'' >> from ...has_and_belongs_to_many_association.rb:5:in >> `initialize'' >> from ...associations.rb:753:in `new'' >> from ...associations.rb:753:in `reviewers_score_cards'' >> from ...associations.rb:749:in `reviewers_score_cards'' >> Here are my models: >> class Reviewer < ActiveRecord::Base >> has_many :candidates >> has_and_belongs_to_many :reviewers_score_cards >> end >> class ScoreCard < ActiveRecord::Base >> has_and_belongs_to_many :reviewers_score_cards, >> :join_table => "reviewers_score_cards" >> end > > That should be something like: > > class Reviewer < ActiveRecord::Base > has_many :candidates > has_and_belongs_to_many :scorecards > end > > class ScoreCard < ActiveRecord::Base > has_and_belongs_to_many :reviewers > end > > And if you name your join table reviewers_scorecards (without the > second underscore) rails should pick it up automatically. > > MichaelSince the model is ScoreCard, the table would be score_cards and the join table would be named reviewers_score_cards. I think the Reviewer model should have: has_and_belongs_to_many :score_cards -Rob