首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails窗体上的Rails集合需要额外的(租户ID)字段

Rails窗体上的Rails集合需要额外的(租户ID)字段
EN

Stack Overflow用户
提问于 2021-06-16 07:57:42
回答 1查看 32关注 0票数 0

我进退两难.我已经把一个多租户的应用程序放在一起了,而且我对一个形式和操作有一个问题。表单(配置文件)有一个集合选择,其中选择适用的技能。有一个多到多的配置文件<- Taggings ->标记。标记记录显示在集合中的select中,启用了多重选择。

当您选择一些技能并保存配置文件记录时,在配置文件更新操作中,Rails抛出:

验证失败:租户必须存在

这似乎来自Taggings表,因为当我删除与租户的关系时,没有错误,配置文件记录成功保存。如果我直接从Rails控制台创建标记记录,tenant_id就会填充并保存记录。

代码语言:javascript
复制
    respond_to do |format|
      if @profile.update(profile_params) < fails here
        format.html { redirect_to profiles_path, notice: 'Profile was successfully updated.' }
        format.json { render :show, status: :ok, location: @profile }
      else

Params (如果我必须在这里插入tenant_id,我希望在标记id数组中插入,但如何插入?):

代码语言:javascript
复制
{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"longobscurestring",
 "profile"=>
  {"description"=>"",
   "tag_ids"=>
    ["",
     "b39b38eb-a90b-434b-9457-1f3b67cee54e",
     "08d90ee7-3194-4fec-acee-bcecfae1e8e7",
     "ee8de96d-1206-4d73-bcf0-0b99f995569a",
     "469ce954-b2bd-49d5-9dbc-0636b4da43c8",
     "38b90691-d3f0-4c9d-8b5f-2c644a894d45",
     "77a332d9-feed-4f88-8133-19066b5d33bc",
     "05c145ce-a8ff-4105-a713-073da60184b5",
     "8d6f98e3-9c3e-4f45-8c7d-36b177b557af"],
   "contact_direct"=>"false"},
 "commit"=>"Save",
 "id"=>"1728fcc4-f2e2-49de-9a39-5c67502b8a85"}

个人资料表格:

代码语言:javascript
复制
  <%= simple_form_for @profile, url: profile_path do |f| %>

    <%= f.input :description, :as => :text, :input_html => {:cols=>100, :rows=>5} %>

    <%= f.input :tag_ids, label: 'Skills', as: :select, collection: Tag.active.order(:name), label_method: :name, input_html: { multiple: true } %>
    <div>
      <span>People may contact me:</br></span>
      <div class="radio-inline margin-10" style="text-align:left">
        <%= f.input :contact_direct, label: '', as: :radio_buttons, collection: [['Directly', true], ['Through my manager', false]] %>
      </div>
    </div>
    <br/>
    <div class="actions">
      <% if ( can? :manage, :all or current_user.id == @profile.user_id) %>
        <%= f.button :submit, 'Save', class: "btn btn-success" %>
      <% end %>
      <%= link_to "Back", profiles_path(:search => {:column => 'First', :direction => 'Up', :name => ''}), class: "btn btn-primary link-as-button" %>
    </div>
  <% end %>

概况模型协会:

代码语言:javascript
复制
class Profile < ApplicationRecord

  belongs_to :user
  belongs_to :tenant
  has_many :taggings, :dependent => :destroy
  has_many :tags, through: :taggings

Taggings示范协会:

代码语言:javascript
复制
class Tagging < ApplicationRecord
  attribute :competence, :integer, default: 0

  belongs_to :tenant
  belongs_to :tag
  belongs_to :profile, optional: true
  has_one :user, through: :profile
  has_many :endorsements, inverse_of: 'tagging'
  has_many :endorsers, through: :endorsements

所有表都通过pg策略实现了RLS。但是,如果没有taggings记录上的tenant_id,则可以通过另一个租户访问该记录。

请告诉我这里还有什么需要调试的。提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2021-06-16 13:01:42

我设法通过在标记模型中添加一个after_initialize回调来解决这个问题:

代码语言:javascript
复制
  def set_tenant_id
    if new_record?
        self.tenant_id = self.tag.tenant_id
    end
  end

请放心,如果这是不好的,practice...but,我目前看不到任何其他方式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67998605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档