<Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org>
2005-Aug-08 15:04 UTC
Lookup Tables/foreign keys
Hi all, I''ve been really stuggling today. I dont know if its the way that I''m approaching things, but after solving the problem with pluralisation, I have been looking for the solution to my second problem: Using a lookup-table for a drop-down list. Basically, I have a table of devices, which have a few forieng keys, such as processor_type_id (which references processor_types(id) ) Now I''d like to have a form that allows you to add a new device to the database with a drop-down list of processor types (P3,P4, AMD etc.). Now I''ve been looking every where for an easy way to do this (I cant be the first person to have tried?). Also, when displaying the list of devices, I''d like it to display the name of the processor (ie by de-referencing the processor_type_id against the processor_types table). Having looked at the scaffolding code, the content_columns method (in ActiveRecord::Base) filters out any thing ending in _id, so in my model I thought I''d overload this method with one that doesnt, and then when displaying the actual values, attempt to de-reference them my self. Is this the correct approach to this problem? Have other people managed this before? If so, then the method to overload when displaying the values would be (as used in the default template generated by scaffold) <% h device.send(column.name) %>. But I cannot find any documentation or source-code for the send() method, which is part of my device class which inherits from ActiveRecord::Base Hopefully I can find a solution to this, otherwise I might have to resort to perl which I already know. Many thanks for any help you may have to offer. ------------------------------------------------------------------------------------------ Equinox Converged Solutions Tel: +44 (0)1252 405 600 http://www.equinoxsolutions.com Equinox Converged Solutions is a trading name of Synetrix Holdings Limited. IMPORTANT NOTICE: This message is intended solely for the use of the Individual or organisation to whom it is addressed. It may contain privileged or confidential information. If you have received this message in error, please notify the originator immediately. If you are not the intended recipient, you should not use, copy, alter, or disclose the contents of this message. All information or opinions expressed in this message and/or any attachments are those of the author and are not necessarily those of Synetrix Holdings Limited. Synetrix Holdings Limited accepts no responsibility for loss or damage arising from its use, including damage from virus. ------------------------------------------------------------------------------------------- _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org wrote:> Hi all, > > I''ve been really stuggling today. I dont know if its the way that I''m > approaching things, > but after solving the problem with pluralisation, I have been looking > for the solution to > my second problem: > > Using a lookup-table for a drop-down list. > > Basically, I have a table of devices, which have a few forieng keys, > such as processor_type_id (which references processor_types(id) ) > > Now I''d like to have a form that allows you to add a new device > to the database with a drop-down list of processor types (P3,P4, AMD etc.). > > Now I''ve been looking every where for an easy way to do this (I cant be > the first > person to have tried?). Also, when displaying the list of devices, I''d > like it to display > the name of the processor (ie by de-referencing the processor_type_id > against > the processor_types table). > > Having looked at the scaffolding code, the content_columns method (in > ActiveRecord::Base) filters out > any thing ending in _id, so in my model I thought I''d overload this > method with one that > doesnt, and then when displaying the actual values, attempt to > de-reference them my self. > > Is this the correct approach to this problem? Have other people managed > this before? > > If so, then the method to overload when displaying the values would be > (as used in the default > template generated by scaffold) <% h device.send(column.name) %>. But I > cannot find > any documentation or source-code for the send() method, which is part of > my device class > which inherits from ActiveRecord::Base > > Hopefully I can find a solution to this, otherwise I might have to > resort to perl which I already know. > > Many thanks for any help you may have to offer. >Is your problem in allowing the user to select an item from a list populated by the lookup table, or to display the selected item by it''s name once it''s parent has been saved? Regards populating a list from lookup table, are you familiar with collection_select? It makes it very easy to create a list from a db lookup table and automatically save the selected value to the parent table in the db. I can help more if I have more info. Chris
Firstly, send() is an object method and is not particular to AR. For your drop-down, you could toss in something like <%= select(''device'', ''processor_type_id'', ProcessorType.find_all.collect{|proc| [proc.title, proc.id]}) %> (<a href="/processor_types/new">New Processor Type?</a>)</p> It''s not elegant, but it shows the idea. You could also do something a little cleaner and more meaningful, such as creating a method of Device such as possible_processor_types... Or, in your controller, do the collection of processor types and pass the list to your view. On Aug 8, 2005, at 11:04 AM, <Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org> <Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org> wrote:> Hi all, > > I''ve been really stuggling today. I dont know if its the way that > I''m approaching things, > but after solving the problem with pluralisation, I have been > looking for the solution to > my second problem: > > Using a lookup-table for a drop-down list. > > Basically, I have a table of devices, which have a few forieng keys, > such as processor_type_id (which references processor_types(id) ) > > Now I''d like to have a form that allows you to add a new device > to the database with a drop-down list of processor types (P3,P4, > AMD etc.). > > Now I''ve been looking every where for an easy way to do this (I > cant be the first > person to have tried?). Also, when displaying the list of devices, > I''d like it to display > the name of the processor (ie by de-referencing the > processor_type_id against > the processor_types table). > > Having looked at the scaffolding code, the content_columns method > (in ActiveRecord::Base) filters out > any thing ending in _id, so in my model I thought I''d overload this > method with one that > doesnt, and then when displaying the actual values, attempt to de- > reference them my self. > > Is this the correct approach to this problem? Have other people > managed this before? > > If so, then the method to overload when displaying the values would > be (as used in the default > template generated by scaffold) <% h device.send(column.name) %>. > But I cannot find > any documentation or source-code for the send() method, which is > part of my device class > which inherits from ActiveRecord::Base > > Hopefully I can find a solution to this, otherwise I might have to > resort to perl which I already know. > > Many thanks for any help you may have to offer. > > > > > > > > Equinox Converged Solutions > Tel: +44 (0)1252 405 600 > www.equinoxsolutions.com > Equinox Converged Solutions is a trading name of Synetrix Holdings > Limited. > > IMPORTANT NOTICE: > This message is intended solely for the use of the Individual or > organisation to whom it is addressed. It may contain privileged or > confidential information. If you have received this message in > error, please notify the originator immediately. If you are not the > intended recipient, you should not use, copy, alter, or disclose > the contents of this message. All information or opinions expressed > in this message and/or any attachments are those of the author and > are not necessarily those of Synetrix Holdings Limited. Synetrix > Holdings Limited accepts no responsibility for loss or damage > arising from its use, including damage from virus. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Using collection_select is another option to this, and is much better. For your use, the code is below. The following var could be created in your controller and passed into the view, but for clarity I''m doing it here. <% @processor_types = ProcessorTypes.find_all %> <%= collection_select(''device'', ''processor_type_id'', @processor_types, ''id'', ''title'') %> (<a href="/processor_types/ new">New Processor Type?</a>)</p> On Aug 8, 2005, at 11:04 AM, <Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org> <Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org> wrote:> Hi all, > > I''ve been really stuggling today. I dont know if its the way that > I''m approaching things, > but after solving the problem with pluralisation, I have been > looking for the solution to > my second problem: > > Using a lookup-table for a drop-down list. > > Basically, I have a table of devices, which have a few forieng keys, > such as processor_type_id (which references processor_types(id) ) > > Now I''d like to have a form that allows you to add a new device > to the database with a drop-down list of processor types (P3,P4, > AMD etc.). > > Now I''ve been looking every where for an easy way to do this (I > cant be the first > person to have tried?). Also, when displaying the list of devices, > I''d like it to display > the name of the processor (ie by de-referencing the > processor_type_id against > the processor_types table). > > Having looked at the scaffolding code, the content_columns method > (in ActiveRecord::Base) filters out > any thing ending in _id, so in my model I thought I''d overload this > method with one that > doesnt, and then when displaying the actual values, attempt to de- > reference them my self. > > Is this the correct approach to this problem? Have other people > managed this before? > > If so, then the method to overload when displaying the values would > be (as used in the default > template generated by scaffold) <% h device.send(column.name) %>. > But I cannot find > any documentation or source-code for the send() method, which is > part of my device class > which inherits from ActiveRecord::Base > > Hopefully I can find a solution to this, otherwise I might have to > resort to perl which I already know. > > Many thanks for any help you may have to offer. > > > > > > > > Equinox Converged Solutions > Tel: +44 (0)1252 405 600 > www.equinoxsolutions.com > Equinox Converged Solutions is a trading name of Synetrix Holdings > Limited. > > IMPORTANT NOTICE: > This message is intended solely for the use of the Individual or > organisation to whom it is addressed. It may contain privileged or > confidential information. If you have received this message in > error, please notify the originator immediately. If you are not the > intended recipient, you should not use, copy, alter, or disclose > the contents of this message. All information or opinions expressed > in this message and/or any attachments are those of the author and > are not necessarily those of Synetrix Holdings Limited. Synetrix > Holdings Limited accepts no responsibility for loss or damage > arising from its use, including damage from virus. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
<Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org>
2005-Aug-08 15:35 UTC
RE: Lookup Tables/foreign keys
Hi Chris, Using the standard scaffolding, I want to edit display/edit a table called ''devices'': CREATE TABLE processor_types ( id serial, processor varchar(255), PRIMARY KEY (id) ); CREATE TABLE devices ( id serial, asset_tag int NOT NULL DEFAULT 0, serial_number varchar(15) DEFAULT NULL, name varchar(20) DEFAULT NULL, ... processor_type_id int DEFAULT NULL REFERENCES processor_types(id), processor_qty smallint DEFAULT NULL, ... PRIMARY KEY (id) ); Now I''d like the list method to display each device''s ''processor'' instead of the processor_type_id (which it doesn''t display any way because the default content_columns() method skips columns who''s names end in ''_id'') And I''d also like the new() method to display a drop-down list of all processor_types so that the user can select one, and have the id be stored back in the database. Does this make more sense? Its my second day of using rails (and Ruby too) so I know I''ve still got a lot to learn. I''ve just looked at collection_select() and I think that this may indeed end up forming part of the solution to this problem. Thanks for your help! -- Brad. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Chris Roos Sent: 08 August 2005 16:11 To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Lookup Tables/foreign keys Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org wrote:> Hi all, > > I''ve been really stuggling today. I dont know if its the way that I''m > approaching things, but after solving the problem with pluralisation, > I have been looking for the solution to my second problem: > > Using a lookup-table for a drop-down list. > > Basically, I have a table of devices, which have a few forieng keys, > such as processor_type_id (which references processor_types(id) ) > > Now I''d like to have a form that allows you to add a new device to the> database with a drop-down list of processor types (P3,P4, AMD etc.). > > Now I''ve been looking every where for an easy way to do this (I cant > be the first person to have tried?). Also, when displaying the list of> devices, I''d like it to display the name of the processor (ie by > de-referencing the processor_type_id against the processor_types > table). > > Having looked at the scaffolding code, the content_columns method (in > ActiveRecord::Base) filters out > any thing ending in _id, so in my model I thought I''d overload this > method with one that doesnt, and then when displaying the actual > values, attempt to de-reference them my self. > > Is this the correct approach to this problem? Have other people > managed this before? > > If so, then the method to overload when displaying the values would be> (as used in the default template generated by scaffold) <% h > device.send(column.name) %>. But I cannot find any documentation or > source-code for the send() method, which is part of my device class > which inherits from ActiveRecord::Base > > Hopefully I can find a solution to this, otherwise I might have to > resort to perl which I already know. > > Many thanks for any help you may have to offer. >Is your problem in allowing the user to select an item from a list populated by the lookup table, or to display the selected item by it''s name once it''s parent has been saved? Regards populating a list from lookup table, are you familiar with collection_select? It makes it very easy to create a list from a db lookup table and automatically save the selected value to the parent table in the db. I can help more if I have more info. Chris _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails ------------------------------------------------------------------------------------------ Equinox Converged Solutions Tel: +44 (0)1252 405 600 http://www.equinoxsolutions.com Equinox Converged Solutions is a trading name of Synetrix Holdings Limited. IMPORTANT NOTICE: This message is intended solely for the use of the Individual or organisation to whom it is addressed. It may contain privileged or confidential information. If you have received this message in error, please notify the originator immediately. If you are not the intended recipient, you should not use, copy, alter, or disclose the contents of this message. All information or opinions expressed in this message and/or any attachments are those of the author and are not necessarily those of Synetrix Holdings Limited. Synetrix Holdings Limited accepts no responsibility for loss or damage arising from its use, including damage from virus. -------------------------------------------------------------------------------------------
Bradley.Kite-93r8m3cLkYPe9wHmmfpqLFaTQe2KTcn/@public.gmane.org wrote:> Hi Chris, > > Using the standard scaffolding, I want to edit display/edit a table > called > ''devices'': > > CREATE TABLE processor_types > ( > id serial, > processor varchar(255), > PRIMARY KEY (id) > ); > > CREATE TABLE devices > ( > id serial, > asset_tag int NOT NULL DEFAULT 0, > serial_number varchar(15) DEFAULT NULL, > name varchar(20) DEFAULT NULL, > ... > processor_type_id int DEFAULT NULL REFERENCES > processor_types(id), > processor_qty smallint DEFAULT NULL, > ... > PRIMARY KEY (id) > ); > > > Now I''d like the list method to display each device''s ''processor'' > instead of the processor_type_id (which it doesn''t display any way > because the default content_columns() method skips columns who''s names > end in ''_id'') > > And I''d also like the new() method to display a drop-down list of all > processor_types so that the user can select one, and have the id be > stored > back in the database. > > Does this make more sense? > > Its my second day of using rails (and Ruby too) so I know I''ve still > got a lot to learn. I''ve just looked at collection_select() and I think > that this may indeed end up forming part of the solution to this > problem. > > Thanks for your help! >I''m assuming that in your models, you have the following relationships. class Device < ActiveRecord::Base belongs_to :processor_type end class ProcessorType < ActiveRecord::Base has_many :devices end This will add the relevant methods to both devices and processortypes to query the associated data. In your new method in your controller, add a line something like. @processortypes = ProcessType.find :all Now in your _form.rhtml partial (assuming you are using standard scaffold generated files), add a line similar to the following. <%= collection_select :device, :processor_type_id, @processortypes, :id, :processor %> This should be all you need to display a list of processor types and also save the relevant id of the selected processor type against a device. When it comes to listing your devices with the relevant processor types, I think you may have to manually add the columns you need (rather than using object.send(method)). To that end, you could have.. <td><%=h device.asset_tag %></td> <td><%=h device.processor_type.processor %></td> To get round having to explicitly call the processor method on processor_type, you can override the to_s method in your ProcessorType model as such. def to_s processor end Hope this helps and makes some sense, Chris