0

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.

1
  • I tried different ways. please post some of your attempt. Commented Jul 4, 2014 at 13:28

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.