Nidhin S G
Nidhin S G

Reputation: 1695

Select Tag with multiple Option in Rails

I have a two tables named Employee and Shop

Class Employee < ActiveRecord::Base
  belongs_to :shop
end

&

Class Shop < ActiveRecord::Base
  has_many :employees
end

An employee say with name abc can have more than one shop if an employee have 10 shops then there will be 10 rows with same employee name but same employeeID(a column present in employee table)

Problem is i have a form (edit employee) where i try to edit an employees detail. So i need to list all the shops inside a select tag with shops that comes under this employeeID as selected. I tried different ways. Not working. please help. Please dont vote me down iff my quiestion is wrong.

Upvotes: 0

Views: 43

Answers (1)

mkis
mkis

Reputation: 441

I'd like to ask you to provide an example of the form you're describing, to make it clear what the problem is.

Another thing is, if Employees can also have many Shops, then it's a many-to-many association. I think you'd be better off setting up your models like this, if that's the case:

Class Employee < ActiveRecord::Base
  has_many :shop_employees
end

Class Shop < ActiveRecord::Base
  has_many :shop_employees
end

Class ShopEmployee < ActiveRecord::Base
  belongs_to :shop
  belongs_to :employee
end

Upvotes: 1

Related Questions