Hi there, I am quite new to RoR, but am an experienced website hacker. I was designing a site for a restaurant and wanted to give them the ability to manage an online menu. After thinking about it for awhile, I decided the most flexible way was to let the users define two things: Menus, and Menuitems. Menuitems can belong to multiple menus, and Menus can have multiple Menuitems. The best model for this in Rails seemed to be has_and_belongs_to_many. However, I couldn''t get anything auto- generated to work for me (perhaps this is my first problem), so I decided to just write it myself. Rather than dump the schema and my ActiveRecord here, I''ve dumped them at the bottom of the e-mail. I''ve successfully done some basic queries using my ActiveRecord classes, so now I''m thinking of the big picture. My big question is, how do I go about architecting this correctly in Rails? I will of course have a page where the users can "CRUD" menus, a page where they can CRUD Menuitems. Then there must be somewhere the users can assign Menuitems to menus, and also look at the reverse relationship (what Menus does this Menuitem belong to?) Before I went about generating controllers willy nilly, I wanted to have some feedback on my approach. I''m acutely aware that there is often a "rails" way of doing things (and of course a "ruby" way as well), and didn''t want to simply fall into my old Perl habits straightaway. Example questions: should I generate one controller to manage everything? Or split the controller between Menus and Menuitems? Can I generate helpful scaffolding for has_and_belongs_to_many type classes, or must I write all the basic CRUD stuff by hand? Is all this contained in the beta rails book and should I buy it? :) Hope someone can steer me in the right direction! Regards, Luke. My schema looks like: CREATE TABLE `menuitems` ( `id` int(11) NOT NULL auto_increment, `shortname` varchar(255) default NULL, `fullname` varchar(255) default NULL, `price` float default NULL, `created_on` timestamp NOT NULL default ''0000-00-00 00:00:00'', `updated_on` timestamp NOT NULL default ''0000-00-00 00:00:00'', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE `menus` ( `id` int(11) NOT NULL default ''0'', `name` varchar(255) default NULL, `created_on` timestamp NOT NULL default ''0000-00-00 00:00:00'', `updated_on` timestamp NOT NULL default ''0000-00-00 00:00:00'', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE `menus_menuitems` ( `menu_id` int(10) unsigned NOT NULL default ''0'', `menuitem_id` int(10) unsigned NOT NULL default ''0'', KEY `menus_menuitems_FKIndex1` (`menu_id`), KEY `menus_menuitems_FKIndex2` (`menuitem_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; My active record looks like: require ''active_record'' class Menu < ActiveRecord::Base has_and_belongs_to_many :menuitems, :join_table => "menus_menuitems" end class Menuitem < ActiveRecord::Base has_and_belongs_to_many :menus, :join_table => "menus_menuitems" end -- Luke Burton. "For relaxing times, make it Suntory time."
Hey Luke-
I think you are on the right track here. One thing I see right
away is your join table. You are using menus_menuitems when you
should be using menuitems_menus. The joined tables need to be in
alphabetical order and i comes before s. Also I cannot recommend the
agile rails book enough. I have been using rails since last november
and I have a pretty good grasp of rails concepts, but the book pulls
it all together so nicely it will save you so much time that it will
pay for itself over and over.
About the way you are modeling things... Are you really going to
need multiple menus? I think the relationship might be easier if your
menuitems belong_to :menus and menus has_many :menu_items. But maybe
I am not seeing something here about your requirements. I am going to
need this exact functionality in a few months when I build a site for
my uncle''s restaurant. So feel free to contact me off of the list and
maybe we can work together on this a little.
Good Luck-
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
509-577-7732
On Jul 4, 2005, at 4:24 AM, Luke Burton wrote:
>
> Hi there,
>
> I am quite new to RoR, but am an experienced website hacker.
>
> I was designing a site for a restaurant and wanted to give them the
> ability to manage an online menu. After thinking about it for
> awhile, I decided the most flexible way was to let the users define
> two things: Menus, and Menuitems. Menuitems can belong to multiple
> menus, and Menus can have multiple Menuitems.
>
> The best model for this in Rails seemed to be
> has_and_belongs_to_many. However, I couldn''t get anything auto-
> generated to work for me (perhaps this is my first problem), so I
> decided to just write it myself.
>
> Rather than dump the schema and my ActiveRecord here, I''ve dumped
> them at the bottom of the e-mail. I''ve successfully done some
basic
> queries using my ActiveRecord classes, so now I''m thinking of the
> big picture.
>
> My big question is, how do I go about architecting this correctly
> in Rails? I will of course have a page where the users can "CRUD"
> menus, a page where they can CRUD Menuitems. Then there must be
> somewhere the users can assign Menuitems to menus, and also look at
> the reverse relationship (what Menus does this Menuitem belong to?)
>
> Before I went about generating controllers willy nilly, I wanted to
> have some feedback on my approach. I''m acutely aware that there is
> often a "rails" way of doing things (and of course a
"ruby" way as
> well), and didn''t want to simply fall into my old Perl habits
> straightaway.
>
> Example questions: should I generate one controller to manage
> everything? Or split the controller between Menus and Menuitems?
> Can I generate helpful scaffolding for has_and_belongs_to_many type
> classes, or must I write all the basic CRUD stuff by hand? Is all
> this contained in the beta rails book and should I buy it? :)
>
> Hope someone can steer me in the right direction!
>
> Regards,
>
> Luke.
>
> My schema looks like:
>
> CREATE TABLE `menuitems` (
> `id` int(11) NOT NULL auto_increment,
> `shortname` varchar(255) default NULL,
> `fullname` varchar(255) default NULL,
> `price` float default NULL,
> `created_on` timestamp NOT NULL default ''0000-00-00
00:00:00'',
> `updated_on` timestamp NOT NULL default ''0000-00-00
00:00:00'',
> PRIMARY KEY (`id`)
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
>
>
> CREATE TABLE `menus` (
> `id` int(11) NOT NULL default ''0'',
> `name` varchar(255) default NULL,
> `created_on` timestamp NOT NULL default ''0000-00-00
00:00:00'',
> `updated_on` timestamp NOT NULL default ''0000-00-00
00:00:00'',
> PRIMARY KEY (`id`)
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
>
>
> CREATE TABLE `menus_menuitems` (
> `menu_id` int(10) unsigned NOT NULL default ''0'',
> `menuitem_id` int(10) unsigned NOT NULL default ''0'',
> KEY `menus_menuitems_FKIndex1` (`menu_id`),
> KEY `menus_menuitems_FKIndex2` (`menuitem_id`)
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
>
> My active record looks like:
>
> require ''active_record''
>
> class Menu < ActiveRecord::Base
> has_and_belongs_to_many :menuitems, :join_table =>
"menus_menuitems"
> end
>
> class Menuitem < ActiveRecord::Base
> has_and_belongs_to_many :menus, :join_table =>
"menus_menuitems"
> end
>
>
>
>
>
> --
> Luke Burton.
>
> "For relaxing times, make it Suntory time."
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Ezra Zygmuntowicz <ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org> writes:> Also I cannot recommend the > agile rails book enough. I have been using rails since last november > and I have a pretty good grasp of rails concepts, but the book pulls > it all together so nicely it will save you so much time that it will > pay for itself over and over.> On Jul 4, 2005, at 4:24 AM, Luke Burton wrote: > >> Example questions: should I generate one controller to manage >> everything? Or split the controller between Menus and Menuitems? >> Can I generate helpful scaffolding for has_and_belongs_to_many type >> classes, or must I write all the basic CRUD stuff by hand? Is all >> this contained in the beta rails book and should I buy it? :)I''ll second the Rails book. It''s tutorial addresses this very issue of when to make controllers. Many of the tutorials out there just make a controller for each model. That sometimes makes sense and sometimes not. The tutorial in the Rails book is an excellent example of having the controllers not one to one with the models. Think of controllers as groups of business flow. Maybe you need a controller for each object, maybe not. My thinking right now is that you don''t need a controller for each model when they are closely associated. In other words, if you need to update two models at the same time (probably because of their relationship) you probably only need one controller to update both. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org