[英]Rails Has many in a belongs_to relationship
In my rails app I have the following models 在我的Rails应用中,我有以下型号
class Member < ActiveRecord::Base
has_many :trainings
end
class Student < ActiveRecord::Base
belongs_to :member
has_many :trainings #maybe a through relationship here
end
class Teacher < ActiveRecord::Base
belongs_to :member
end
######edited################# ###### edited #################
class Training < ActiveRecord::Base
belongs_to :member #only member not student nor teacher
end
############################# ############################
Now, how can I build the trainings in my student controller 现在,我该如何在学生控制器中进行培训
class StudentsController < ApplicationController
def new
@student = Student.new
@student.trainings.build #### This is not working
end
end
Thanks 谢谢
You have to write accepts_nested_attributes_for in the model and add them in strong parameters if you are using rails 4. Like this : 如果使用rails 4,则必须在模型中编写accepts_nested_attributes_for并将其添加到强参数中。
class Student < ActiveRecord::Base
belongs_to :member
has_many :trainings
accepts_nested_attributes_for :trainings
end
class StudentsController < ApplicationController
def new
@student = Student.new
@student.trainings.build
end
def create
@student = Student.create(student_params)
@student.trainings.build(params[:student][:trainings])
redirect_to student_path
end
#For rails 4
def student_params
params.require(:student).permit(:id, :name, trainings_attributes: [ :id, :your fields here ])
end
end
Here is a link that will help you: Rails 4: accepts_nested_attributes_for and mass assignment 这是一个对您有帮助的链接: Rails 4:accepts_nested_attributes_for和批量分配
If you've properly defined your associations, then the code in your new
controller action will work (I tested it). 如果您已经正确定义了关联,那么
new
控制器操作中的代码将起作用(我已对其进行了测试)。 Check and make sure your Training
model exists, or that you've used the correct association name (perhaps you meant :teachers
?). 检查并确保您的
Training
模型存在,或者您使用了正确的关联名称(也许您是指:teachers
?)。
app/models/student.rb app / models / student.rb
class Student < ActiveRecord::Base
has_many :trainings
end
app/models/training.rb app / models / training.rb
class Training < ActiveRecord::Base
belongs_to :student
end
app/controllers/students_controller.rb app / controllers / students_controller.rb
class StudentsController < ApplicationController
def new
@student = Student.new
@student.trainings.build
end
end
Update: 更新:
Assuming these are how your associations are defined, you could build a scoped instance of Training
like so: 假设这些是您的关联的定义方式,则可以像下面这样来构建
Training
的作用域实例:
app/models/member.rb app / models / member.rb
class Member < ActiveRecord::Base
has_many :trainings
end
app/models/student.rb app / models / student.rb
class Student < ActiveRecord::Base
delegate :trainings, to: :member
belongs_to :member
end
app/models/training.rb app / models / training.rb
class Training < ActiveRecord::Base
belongs_to :member
end
app/controllers/students_controller.rb app / controllers / students_controller.rb
class StudentsController < ApplicationController
def new
@student = Student.new
@student.build_member
@student.trainings.build
end
end
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.