Does anyone have an example of the proper usage of collection_select? -- Thanks. Lon baker
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Lon Baker wrote: | Does anyone have an example of the proper usage of collection_select? class User < ActiveRecord::Base ~ # id, name fields ~ belongs_to :group end class Group < ActiveRecord::Base ~ # id, name fields ~ has_many :users end class UserController < AbstractApplicationController ~ model :user ~ def edit ~ @user = User.find(@params[''id'']) ~ end end views/user/edit.rhtml <%= collection_select ''user'', ''group_id'', Group.find_all, ''id'', ''name'' %> This invocation will generate a select tag name="user[group_id]" with options of form <option value="group.id">group.name</option>. The option with group.id == user.group_id will be selected. Hope this helps, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBo8vpAQHALep9HFYRAl//AJ9Zy4RlrqENap2rH+c4dYRiBP0jHACgnGsV EP3kybdAZwBbmnd0WppX+hs=GUzz -----END PGP SIGNATURE-----
Nice, are there any functions like this for handling one to many relations? i.e a User has_and_belongs_to_many :groups. On Tue, 23 Nov 2004 15:46:50 -0800, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Lon Baker wrote: > | Does anyone have an example of the proper usage of collection_select? > > class User < ActiveRecord::Base > ~ # id, name fields > ~ belongs_to :group > end > > class Group < ActiveRecord::Base > ~ # id, name fields > ~ has_many :users > end > > class UserController < AbstractApplicationController > ~ model :user > ~ def edit > ~ @user = User.find(@params[''id'']) > ~ end > end > > views/user/edit.rhtml > <%= collection_select ''user'', ''group_id'', Group.find_all, ''id'', ''name'' %> > > This invocation will generate a select tag name="user[group_id]" with > options of form <option value="group.id">group.name</option>. The > option with group.id == user.group_id will be selected. > > Hope this helps, > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.6 (Darwin) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFBo8vpAQHALep9HFYRAl//AJ9Zy4RlrqENap2rH+c4dYRiBP0jHACgnGsV > EP3kybdAZwBbmnd0WppX+hs> =GUzz > -----END PGP SIGNATURE----- > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Michael Koziarski wrote: | Nice, are there any functions like this for handling one to many | relations? i.e a User has_and_belongs_to_many :groups. collection_select requires a source object and an instance method. With has_many, this method can be the foreign key. But with has_and_belongs_to_many there is no corresponding instance method since the keys are in a separate join table, so you''ll want to manage the parameters yourself. Here''s an example using options_from_collection_for_select to create a multiple select: class User < ActiveRecord::Base ~ # id, name fields ~ has_and_belongs_to_many :groups end class Group < ActiveRecord::Base ~ # id, name fields ~ has_and_belongs_to_many :users end class UserController < AbstractApplicationController ~ model :user ~ def edit ~ case @request.method ~ when :get ~ @user = User.find(@params[''id'']) ~ @available_groups = Groups.find_all - @user.groups ~ when :post ~ @user = User.find(@params[''user''][''id'']) ~ @user.groups << Group.find(@params[''add_groups'']) ~ redirect_to :action => ''show'', :id => @user.id ~ end ~ end end views/user/edit.rhtml <%= form_tag :action => ''edit'' %> ~ <%= hidden_field ''user'', ''id'' %> ~ <select name="add_groups[]" multiple="multiple"> ~ <%= options_from_collection_for_select @available_groups, ''id'', ''name'' %> ~ </select> </form> Check out the relevant FormOptionsHelper documentation: http://ap.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000075 Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBpBYeAQHALep9HFYRAk8qAKCbRIzv2GBiWGSfIsmlx1ZZYuodqQCePlV2 9tqGiLfiZTyocwJ1U9xMTmY=vrPm -----END PGP SIGNATURE-----
These two statements are pretty much exactly what I''m after. @user.groups << Group.find(@params["add_ids"]) @user.groups.delete(Group.find(@params["delete_ids"]) However, looking at the logs makes me think it''s doing SELECT* from groups WHERE ... INSERT INTO user_groups ...... The select''s seem rather redundant here. Am I misreading the logs? or is there a better way of doing this? perhaps making find lazily load the objects? On Tue, 23 Nov 2004 21:03:26 -0800, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Michael Koziarski wrote: > | Nice, are there any functions like this for handling one to many > | relations? i.e a User has_and_belongs_to_many :groups. > > collection_select requires a source object and an instance method. With > has_many, this method can be the foreign key. But with > has_and_belongs_to_many there is no corresponding instance method since > the keys are in a separate join table, so you''ll want to manage the > parameters yourself. Here''s an example using > options_from_collection_for_select to create a multiple select: > > class User < ActiveRecord::Base > ~ # id, name fields > ~ has_and_belongs_to_many :groups > end > > class Group < ActiveRecord::Base > ~ # id, name fields > ~ has_and_belongs_to_many :users > end > > class UserController < AbstractApplicationController > ~ model :user > ~ def edit > ~ case @request.method > ~ when :get > ~ @user = User.find(@params[''id'']) > ~ @available_groups = Groups.find_all - @user.groups > ~ when :post > ~ @user = User.find(@params[''user''][''id'']) > ~ @user.groups << Group.find(@params[''add_groups'']) > ~ redirect_to :action => ''show'', :id => @user.id > ~ end > ~ end > end > > views/user/edit.rhtml > <%= form_tag :action => ''edit'' %> > ~ <%= hidden_field ''user'', ''id'' %> > ~ <select name="add_groups[]" multiple="multiple"> > ~ <%= options_from_collection_for_select @available_groups, ''id'', > ''name'' %> > ~ </select> > </form> > > Check out the relevant FormOptionsHelper documentation: > http://ap.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000075 > > Best, > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.6 (Darwin) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFBpBYeAQHALep9HFYRAk8qAKCbRIzv2GBiWGSfIsmlx1ZZYuodqQCePlV2 > 9tqGiLfiZTyocwJ1U9xMTmY> =vrPm > -----END PGP SIGNATURE----- >-- Cheers Koz