【发布时间】:2016-12-27 11:02:43
【问题描述】:
我有两个表 Role 和 User,我将这两个表与 rails 的 has_and_belongs_to_many 关系链接起来。
我成功地将数据插入到由 has_and_belongs_to_many 关系创建的第三个表中。使用以下代码
def create
user_params[:password] = User.hash(user_params[:password])
@user = User.new(:first_name => user_params[:first_name],
:last_name=>user_params[:last_name],
:email => user_params[:email],
:contact_number=>user_params[:contact_number],
:password=>user_params[:password])
@roles = user_params[:roles];
for role in @roles
@user.roles << Role.find(role)
end
if @user.save
respond_to do |format|
msg = { :status => "ok", :message => "User Creation Success!" }
format.json { render :json => msg }
end
end
end
现在我的问题是如何从关系表中读取值以及如何将任何值更新到关系表中。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-5