ruby on rails - ActiveRecord association model -
I'm new to the rail and read all the information so far to get it.
I have a simple scenario and to make sure that my institutions work well.
Scenario: Login to user
-> Many sets groups
-> There are many employees in each group
User
Model:
class user & lt; ActiveRecord :: Base is has_many: group end
group
model:
class group
Employee
Model:
Class Employees & lt; ActiveRecord :: Base has has_many: group is_to: group end
question
- Have I worked for this scenario ?
- I'm confused about how to get all
employees
under auser
. What will be the code for this? - If I need a specific CRUDE for all these models, will my action be? Index / create / update / delete? Can someone tell me a verb in a good guide?
I also like -
class Users & lt; ActiveRecord :: Base has has_many: Groups have has_many: Employees: By: = By & gt ;: Group End
Then you can go:
User = user.find (23) user.employees.do_something
Otherwise, you can loop through your groups and its employees (some ugly, but will work)
User first.groups.each {| Group | Group.employees.each {| Employees | Puts the employee Name}}
Comments
Post a Comment