开发者

whats the difference between these create methods

Hey I am stuck with my orientation in rails.

I got a User model, a Course Model and a CourseEnrollment Model.

When I want to add a link in my Course Index View like

link_to 'join' CourseEnrollment.create(:course_id => course.id, :user_id => current_user)

Does this create method belong to my Model? I am confused because in my User Model I defined a method that uses role_assignme开发者_StackOverflownts.create(.....). What is the difference between these 2 create methods? I cant use course_enrollments.create by the way. Thx for your time


I'm a bit confused as to what you're asking, but I'll try my best.

(First of all, in your example, current_user should probably be current_user.id.)

When you call CourseEnrollment.create, you are simply creating a new CourseEntrollment model with the specified attributes.

Assuming that your User model has_many :role_assignments:

When you call @role_assignments.create from within your User model, Rails automatically creates the association for you (e.g. sets the user_id to the id of the user). This doesn't have to be done within the model itself, though:

current_user.role_assignments.create(...) # automatically sets the association

Assuming that your User model also has_many :course_enrollments, the following will create a CourseEnrollment model and automatically associate it with the current user:

current_user.course_enrollments.create(...)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜