I finally got around my database setup problems on OS-X and have rocked along in the OnLamp tutorial up to the point of adding a new recipe with a category assigned. Before getting to the error message let me state that I am using CocoaMySQL for database construction and maintenance for the tutorial. When creating a new recipe and clicking on the "Create" button I get the following error: ActiveRecord::AssociationTypeMismatch in Recipe#create Category expected, got String script/server:49 Me thinks it is probably in the way I created the category id field in both tables. I used the default parameters that CocoaMySQL creates for the "id" field in the categories table. It is different from what MySQL-Front creates in that it doesn''t capitalize "id"(good!) and uses an int(11) NULL allowed with default value of NULL. I changed the NULL allowed setting to NO(but couldn''t change the default). When creating the connecting field in the recipes table I matched the settings for "id" in the categories table. I also made the "id" field in the categories table the primary key. When using the web form to create the new recipe I tried passing a known "id" value and got the above error. Any ideas? By the way, I did double check the relationship declarations in both models and that seems consistent with the example. Maybe the int value for the "id" field makes a difference? -- Best Regards, Jody Leavell
On 10.2.2005, at 20:23, Jody Leavell wrote:> > Me thinks it is probably in the way I created the category id field in > both tables. I used the default parameters that CocoaMySQL creates for > the "id" field in the categories table. It is different from what > MySQL-Front creates in that it doesn''t capitalize "id"(good!) and uses > an int(11) NULL allowed with default value of NULL. I changed the NULL > allowed setting to NO(but couldn''t change the default). When creating > the connecting field in the recipes table I matched the settings for > "id" in the categories table. I also made the "id" field in the > categories table the primary key. > > When using the web form to create the new recipe I tried passing a > known "id" value and got the above error. Any ideas? By the way, I did > double check the relationship declarations in both models and that > seems consistent with the example. Maybe the int value for the "id" > field makes a difference?This might not have anything to do with your problem, but do you have auto_increment turned on for your primary key fields? It''s in the ''extra'' column of the table structure view in CocoaMySQL. But your problem actually seems to be that your code expects to get a Category object but it''s instead getting a String (maybe the category_id). I haven''t the tutorial but you should probably look at the place in your controller where you set the category of the recipe in the create action. Just guessing, but maybe you have something like @recipe.category = @params["category_id"] there. Try changing it to @recipe.category = Category.find(@params["category_id"]) and see if that helps. //jarkko> -- > Best Regards, > > > Jody Leavell > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I just got back into it and it now works. The one change that I did make was to rename the foreign key field in the recipes table "category_id" instead of just "category". I restarted the webbrick server from the script file last night but that didn''t have an effect. Maybe after shutting down then rebooting this morning the field name change took effect. I learned the importance of the "fkey_id" syntax from the 24slash7 article linked from the Rails home page. Thanks for making time for me. So far I have found the Ruby community very friendly. ps: I will cross post my mistake and correction to the list. Andrew Otwell wrote: >No the tutorial works ok, I have almost the identical setup as you and >I have a working cookbook. Where does it seem to be going wrong? Best Regards, Jody Leavell