Cyber
Cyber

Reputation: 5000

collection_select using formtastic_form

I am having two tables: company and department. In company table i have "departmentname" field for saving department name taken from department table. I am using formtastic form for collection select in my view.

<%= set.input :departmentname ,:as => :select, :collection => Department.all) %> 

i am getting department list, in my view. After saving "departmentname" field in Company Table is having department id and not name of the department.

How to save department name rather than id in my company table.

Thanks in advance.

Upvotes: 0

Views: 238

Answers (1)

HungryCoder
HungryCoder

Reputation: 7616

what is name of the field in Department table whose value will be in departmentname in Company table? I guess it is name and your department table structure is like:

id    name
1     department 1
2     department 2

Try it as below:

:collection => Hash[Department.all.map{|d| [d.name,d.name]}]

however, I guess the following will work too (not sure though)

:collection => Hash[Department.all.map{|d| [d.name]}]

Upvotes: 1

Related Questions