【发布时间】:2014-03-20 04:12:35
【问题描述】:
我之前创建了以下没有 has_many: 和 belongs_to: 属性的模型。他们之前工作得很好,但在添加 has_many 和 belongs_to 之后就不行了,它不起作用。我有一个联合表,然后试图通过 has_many: 和 belongs_to: 将其与用户关联,如下所示。
这是属于教室的:
belongs_to :user
belongs_to :first
belongs_to :second
belongs_to :third
当我之前通过
创建对象时attributes = user_params
@user = User.find_by_id(attributes[:user_id].to_i)
attributes = classroom_params
@classroom = @user.classroom.where(:first_id => attributes[:first].to_i,
:second_id => attributes[:second].to_i,
:third_id => attributes[:third].to_i).first_or_create
这不起作用后,我尝试了以下方法:
@classroom = @user.classrooms.create(:first_id => attributes[:first].to_i,
:second_id => attributes[:second].to_i,
:third_id => attributes[:third].to_i)
并得到了这个错误,即使它以前工作过:
ActiveRecord::UnknownAttributeError (unknown attribute: first_id):
app/controllers/api/v1/classroom_controller.rb:30:in `create'
我想让它与 first_or_create 一起使用,但第二种方法是一个不错的起点
【问题讨论】:
标签: ruby-on-rails activerecord