Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown-Select for a database table

I simply want to have a dropdown menu which will have all the rows of a table as its choices. How do I achieve that? Let's say I have a model "Student" and I want to have the "name" property to be displayed in the choices.

Thanks in advance.

like image 265
Sammy Avatar asked Apr 28 '13 06:04

Sammy


People also ask

How do I create a dropdown column in SQL?

You cannot "set a field to be a dropdown" in SQL Server. You can limit the acceptable values of this field in SQL Server. Then, in your UI language (C#, VB, ...) you can define a drop-down list which can be filled with values from this field. You can also do this in SQL Server Management Studio.

What is the data type for dropdown list in SQL?

The default "Dropdown list" data-type will save the value you select - which would usually be the display text. Whereas the "Dropdown list with publishing keys" saves the PreValue id (from the database) instead of the text. You could use "umbraco. library.

How do you select data from a database?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.


2 Answers

Use select, for example:

select(:model, :attribute, Student.all.collect {|p| [ p.name, p.id ] })
like image 82
Anton Grigoryev Avatar answered Oct 22 '22 04:10

Anton Grigoryev


Here is some info on using collection selects...

http://www.fmhcc.com.au/ruby/rails/using-collection_select-in-rails/

like image 33
muttonlamb Avatar answered Oct 22 '22 04:10

muttonlamb