has_many通过表单并向联接表添加属性 [英] has_many through form and adding attribute to join table

查看:64
本文介绍了has_many通过表单并向联接表添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个stakoverflow问题,对Rails来说还很陌生.我已经搜索过以前的类似问题,但似乎无法弄清楚这个问题.我通过与用户和帐户的关系拥有has_many,并且在UserAccount模型(联接表)上有一个额外的布尔属性"account_admin".我想弄清楚在创建新帐户时如何进行设置.

This is my first stakoverflow question and fairly new to Rails. I've searched through previous similar questions but can't seem to figure this one out. I have a has_many through relationship with Users and Accounts and I have an extra boolean attribute "account_admin" on the UserAccount model (join table). I'm trying to figure out how to set this when I create a new Account.

用户:

class User < ActiveRecord::Base
  has_many :user_accounts, :dependent => :destroy
  has_many :accounts, :through => :user_accounts
  ..
end

帐户:

class Account < ActiveRecord::Base
  has_many :user_accounts, :dependent => :destroy
  has_many :users, :through => :user_accounts
end

UserAccount:

UserAccount:

class UserAccount < ActiveRecord::Base
  belongs_to :user
  belongs_to :account
  # includes account_admin boolean
end

我希望能够以一种形式创建一个帐户,将用户分配给该帐户,并指定一个account_admin.到目前为止,我已经可以选择帐户中的用户了,但是我不确定如何在此处设置account_admin布尔值.

I would like to be able to create an account, assign users to that account, and designate an account_admin all in one form. I have this so far which allows me to select the users in an account but I'm not sure how to set the account_admin boolean here as well.

= simple_form_for @account do |f|
  = f.input :name
  = f.input :description
  = f.association :users, label_method: :to_label, :as => :check_boxes
  = f.button :submit

感谢所有提示.谢谢

推荐答案

帐户模型:

class Account < ActiveRecord::Base
  attr_accessible :name, :description,  :user_accounts_attributes
  has_many :user_accounts, :dependent => :destroy
  has_many :users, :through => :user_accounts
  has_many :user_without_accounts, :class_name => 'User', :finder_sql => Proc.new {
       %Q{
         SELECT *
         FROM users where id NOT IN (Select user_id from user_accounts where account_id = #{id})
       }
   }

  accepts_nested_attributes_for :user_accounts, reject_if:  proc { |attributes| attributes['user_id'] == '0' }

 def without_accounts
    new_record? ? User.all :  user_without_accounts
 end
end

形式:

= simple_form_for @account do |f|
  = f.input :name
  = f.input :description
  - @account.user_accounts.each do |user_account|
  = f.simple_fields_for :user_accounts, user_account do |assignment|
    = assignment.check_box :user_id
    = assignment.label :user_id, user_account.user.name rescue raise  user_account.inspect
    = assignment.input :account_admin
    %hr
  - @account.without_accounts.each do |user|
    = f.simple_fields_for :user_accounts, @account.user_accounts.build do |assignment|
    = assignment.check_box :user_id
    = assignment.label :user_id, user.name
    = assignment.input :account_admin
    %hr
  = f.button :submit

这篇关于has_many通过表单并向联接表添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆