我的ROR 4应用程序存在问题。这是一个快速背景:
该应用有几个类,Users
,Training_Events
和Mission_Notes
。
Training
事件可以与多选下拉列表中的多个用户相关联,该下拉列表构建一个user_id数组,然后保存到Training_Event
,而Mission_Notes
只能User
与一个Training_Event
和一个 class MissionNote < ActiveRecord::Base
belongs_to :training_event
belongs_to :user
end
相关联。以下型号:
MissionNote.rb
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
belongs_to :group
has_many :ranks
has_many :mission_notes
has_and_belongs_to_many :training_events
validates :username, presence: true
validates :email, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :group_id, presence: true
has_secure_password
validates :password, length: { minimum: 6 }, allow_blank: true
end
User.rb
class TrainingEvent < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :mission_notes
validates :title, presence: true
validates :date, presence: true
validates :mission, presence: true
validates_format_of :video, :with => /\A(https\:\/\/)?((www\.)?youtube\.com|youtu\.?be)\/.+$\Z/, :allow_blank => true, :message => "must be a valid YouTube URL"
validates_format_of :date, :with => /\A((19|20)\d\d+)-(0[1-9]|1[012]+)-(0[1-9]|[12][0-9]|3[01])\Z/
TrainingEvent.rb
user_id
端
我当时希望它做的是在用户的个人资料中显示特定用户与之关联的事件列表以及每个事件的mission_notes。我遇到的问题是,当我保存训练事件时,TrainingEvent.all.each{|x| x.user_ids}
字段未保存在数据库中,但如果我user_ids
,则会得到一个已保存的user_id
数组。 / p>
有人可以帮助指出我在这里做错了什么,并且可能有助于澄清单class TrainingEventsController < ApplicationController
before_action :set_training_event, only: [:show, :edit, :update, :destroy]
before_action :admin_user, only: [:new, :edit, :update]
# GET /training_events
# GET /training_events.json
def index
@training_events = TrainingEvent.all
end
# GET /training_events/1
# GET /training_events/1.json
def show
@user_ids = @training_event.user_ids
@user = User.find(@user_ids)
@mission_notes = MissionNote.find_by(user_id: @user)
byebug
end
# GET /training_events/new
def new
@training_event = TrainingEvent.new
@user_options = User.all.map{|u| [ u.username, u.id ] }
end
# GET /training_events/1/edit
def edit
@user_options = User.all.map{|u| [ u.username, u.id ] }
end
# POST /training_events
# POST /training_events.json
def create
@training_event = TrainingEvent.new(training_event_params)
respond_to do |format|
if @training_event.save
format.html { redirect_to @training_event, notice: 'Training event was successfully created.' }
format.json { render :show, status: :created, location: @training_event }
else
format.html { render :new }
format.json { render json: @training_event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /training_events/1
# PATCH/PUT /training_events/1.json
def update
respond_to do |format|
if @training_event.update(training_event_params)
format.html { redirect_to @training_event, notice: 'Training event was successfully updated.' }
format.json { render :show, status: :ok, location: @training_event }
else
format.html { render :edit }
format.json { render json: @training_event.errors, status: :unprocessable_entity }
end
end
end
# DELETE /training_events/1
# DELETE /training_events/1.json
def destroy
@training_event.destroy
respond_to do |format|
format.html { redirect_to training_events_url, notice: 'Training event was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_training_event
@training_event = TrainingEvent.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def training_event_params
params.require(:training_event).permit(:title, :date, :training_objective, :mission, :video, :user_ids => [])
end
end
找不到任何东西,但user_ids至少返回一个项目数组。
-------------------编辑--------------------------- --------- Training_Events_Controller.rb
{{1}}
另外作为一个额外的可以有人建议最好的方式,然后以我上面描述的方式把它放在视图上,我意识到这部分有点模糊,但它已经让我绕过弯道,因为我似乎得到一个重复事件和笔记列表。当我有第二个
时会附加视图代码答案 0 :(得分:1)
由于您使用的是HABTM,因此您将无法使用&#34; User_ID&#34; ...您将拥有&#34; user_ids&#34;因为你告诉它有一个描述多对多关系的连接表。我怀疑当你保存培训活动时,你正试图更新一个&#34; user_id&#34;您应该将当前user_id添加到表示关系的user_ids数组属性中。 :)
希望这有帮助!
答案 1 :(得分:0)
模型TrainingEvent
具有属性user_ids
,我假设它是一个数组而不是user_id
。
我的建议是接收user_ids
未保存但使用attr_accessor :user_ids
访问的数组,然后创建训练事件,遍历user_ids并使用相应的user_id
保存每个TrainingEvent。
确保user_id
是属性,而不是user_ids
。