Ruby on rails 基于丰富关系属性的极限关联

Ruby on rails 基于丰富关系属性的极限关联,ruby-on-rails,ruby,ruby-on-rails-3,simple-form,slim-lang,Ruby On Rails,Ruby,Ruby On Rails 3,Simple Form,Slim Lang,这里是我的第一个问题!我试图将“关键公司”分配给我的用户。它们可以在多对多富连接表中找到。此表中有诸如new、key、active等属性。我想将公司分配给长列表中的用户,为此我使用SimpleForm 除了我想根据rich关系上的属性过滤和限制关联关系之外,一切都正常工作。我为每个用户建立了公司关系,但并非所有的关系都是键关系或新关系。我只希望关联显示为键,而不触摸其他关联。当我将这些公司分配给用户时,我还想将属性active设置为true。我的代码现在看起来像这样: user.rb class

这里是我的第一个问题!我试图将“关键公司”分配给我的用户。它们可以在
多对多
富连接表中找到。此表中有诸如
new
key
active
等属性。我想将公司分配给长列表中的用户,为此我使用
SimpleForm

除了我想根据rich关系上的属性过滤和限制关联关系之外,一切都正常工作。我为每个用户建立了公司关系,但并非所有的关系都是
键关系或
新关系。我只希望关联显示为
,而不触摸其他关联。当我将这些公司分配给用户时,我还想将属性
active
设置为
true
。我的代码现在看起来像这样:

user.rb

class User < ActiveRecord::Base
    has_many :company_user_relationships
    has_many :companies, through: :company_user_relationships
class Company < ActiveRecord::Base
    has_many :company_user_relationships
    has_many :users, through: :company_user_relationships
create_table "company_user_relationships", force: true do |t|
  t.integer  "company_id"
  t.integer  "user_id"
  t.boolean  "key"
  t.boolean  "active"
  t.datetime "last_contacted"
  t.string   "status_comment"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "status"
  t.boolean  "new"
end
def assign_key_companies
    User.update(params[:users].keys, params[:users].values)
    redirect_to(:back)
end
用户\u控制器.rb

class User < ActiveRecord::Base
    has_many :company_user_relationships
    has_many :companies, through: :company_user_relationships
class Company < ActiveRecord::Base
    has_many :company_user_relationships
    has_many :users, through: :company_user_relationships
create_table "company_user_relationships", force: true do |t|
  t.integer  "company_id"
  t.integer  "user_id"
  t.boolean  "key"
  t.boolean  "active"
  t.datetime "last_contacted"
  t.string   "status_comment"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "status"
  t.boolean  "new"
end
def assign_key_companies
    User.update(params[:users].keys, params[:users].values)
    redirect_to(:back)
end
查看

= form_for :user_companies,
        url: assign_key_companies_users_path,
        html: {:method => :put} do |f|
  - users.each do |user|
    = simple_fields_for "users[]", user do |u|
      tr
        td.col-md-4
          = "#{user.first_name} #{user.last_name}"
        td.col-md-8
          = u.association :companies, label: false, collection: @key_company_candidates,
                      input_html: {data: {placeholder: " Assign key companies"}, class: 'chosen-select'}
  = submit_tag "Save key companies", class: "btn btn-success pull-right"
我基本上只想显示
user.companys.where(key:true)
和SQL
COMMIT
,以便在更新记录时始终将
key
-字段设置为
true


如何筛选出只影响我想要的关联?

我可以想出两种方法

首先在关联级别对其进行筛选

class User < ActiveRecord::Base
    has_many :company_user_relationships, -> { where("company_user_relationships.key" => true) }

当您调用
user.companys
时,它实际上在所有三个表中执行联接表,因此您可以像我的示例那样指定条件。

我可以想出两种方法

首先在关联级别对其进行筛选

class User < ActiveRecord::Base
    has_many :company_user_relationships, -> { where("company_user_relationships.key" => true) }

当您调用
user.companys
时,它实际上在所有三个表中执行联接表,因此您可以像我的示例那样指定条件。

谢谢!我找到了一种方法来添加另一个只显示关键关系的has__许多关系,有点像您显示的第一个示例。通过rails魔术,当我批量分配属性时,它会自动为我设置所有属性:)谢谢!我找到了一种方法来添加另一个只显示关键关系的has__许多关系,有点像您显示的第一个示例。通过rails magic,当我批量分配属性时,它会自动为我设置所有属性:)