[英]Ruby on Rails has_many :through Association controller
these are my 3 models : 这是我的3个模型:
model for User: 用户模型:
class User < ActiveRecord::Base
has_many :patients, through: :treatments
has_many :treatments
.
.
.
model for patient: 病人模型:
class Patient < ActiveRecord::Base
has_many :user, through: :treatments
has_many :treatments, dependent: :destroy
.
.
.
model for treatment: 治疗模式:
class Treatment < ActiveRecord::Base
belongs_to :patient
belongs_to :user
validates :patient_id, presence: true
default_scope -> { order(created_at: :desc) }
end
And this is my treatment table : 这是我的治疗表:
class CreateTreatments < ActiveRecord::Migration
def change
create_table :treatments do |t|
t.date :teartment_date
t.text :remark
t.float :fee
t.references :patient, index: true, foreign_key: true
t.timestamps null: false
end
add_index :treatments, [:patient_id, :created_at]
end
end
now i want to define a controller to create a new treatment that belongs to a specific user's patient. 现在,我想定义一个控制器来创建属于特定用户患者的新治疗方法。
this is my controller : 这是我的控制器:
def new
@treat = Treatment.new
end
def create
@userpatient = current_user.treatments.build(treat_params)
if @userpatient.save
flash[:success] = "new treatment added"
redirect_to root_url
else
render 'new'
end
end
but this is the error that i receive, while i want to create a new treatment : 但这是我想要创建新治疗方法时收到的错误:
ActiveRecord::UnknownAttributeError in TreatmentsController#create
unknown attribute 'user_id' for Treatment.
and this is the current_user : 这是current_user:
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
@current_user = user
end
end
end
i'm new to rails, the basic idea is i want my user to have treatment that belongs to a specific patient. 我是Rails的新手,基本思想是我希望我的用户接受属于特定患者的治疗。
Thanks to replies i've over come with this issue by adding a reference column . 感谢答复,我通过添加参考列解决了这个问题。 now i receive no, but it does not save any treatments.
现在我没有收到任何通知,但它不保存任何治疗。 i mean the part :
我的意思是:
if @treat.save
flash[:success] = "new treatment added"
redirect_to root_url
else
render 'new'
end
it does not save and just render 'new' . 它不会保存,而只是渲染'new'。
i have 2 questions : 我有2个问题:
1- how can i code my create controller ? 1-我如何编码我的创建控制器?
2- how to retrieve my treatments base on patient.what variable should i define in my patient 'show' method to have its treatments retrieved ? 2-如何基于患者检索治疗方法。我应该在患者的“显示”方法中定义什么变量以检索其治疗方法?
When you say that User
has_many :treatments
and that Treatment
belongs_to :user
, both associations are expecting to find a user_id
column in your treatments
table. 当你说
User
has_many :treatments
和Treatment
belongs_to :user
,这两个协会都希望找到一个user_id
在列treatments
表。 You might want to change your migration to include: 您可能需要更改迁移以包括:
t.integer :user_id
and then drop your tables (if they have no data yet!) and rerun the migrations. 然后删除表(如果还没有数据!),然后重新运行迁移。 Alternatively, you could create a new migration and simply run that:
另外,您可以创建一个新的迁移并简单地运行它:
add_column :treatments, :user_id, :integer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.