开发者

has_many in rails

开发者 https://www.devze.com 2023-01-03 05:29 出处:网络
I have a \'users\' table which stores all of my websites users, some of which are technicians. The users table has a \'first_name\' and \'last_name\' field as well as other fields.

I have a 'users' table which stores all of my websites users, some of which are technicians. The users table has a 'first_name' and 'last_name' field as well as other fields.

The other table开发者_如何转开发 is called 'service_tickets' which has a foreign key from users called technician_id. This gives me a real problem because when I am looking at the service_tickets table, the related user is actually a technician. That's why the service_tickets table has a technician_id and not a user_id field.

I am trying to accomplish something like this:

t = service_ticket.find_by_id(7)
t.technician.first_name        # notice how I don't do t.user.first_name

Is this possible in rails? I can't seem to get it to work...

Thank you for your help!


In your service ticket model, you can add a technician relationship like...

belongs_to :technician, :class_name => "User"

It will use the User model in this case for the technician_id

0

精彩评论

暂无评论...
验证码 换一张
取 消