简体   繁体   中英

Ruby on Rails adding relationship doesn't update new form

Basic rails question.

I have a class, let's say User, and another class, Category. I set up a relationship between them, so that Users have categories and categories have users. When I go to users/new to create a new user though, there is no prompt for category. I'm expecting the rails magic to kick in and have done that, so I'm wondering if my expectations were wrong or if I haven't executed something properly.

You should set your User model to belong_to:category, and the Category to have_many:users.

That way, when you want to create a new user, it will ask you what category the user belongs to. You will probably need to run a migration to add the category_id index to the users table though. That will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM