簡體   English   中英

更新以相同形式Rails 5創建的多個模型的用戶屬性

[英]Updating user attributes for multiple models being created in same form Rails 5

我有一種奇怪的情況(至少我認為這很奇怪..)

當我在應用中創建新帳戶時,我也會創建帳戶所有者(設計用戶)

我遇到問題的地方是我需要將account_id添加到所有者記錄,並將owner_id添加到帳戶記錄。 所有者隨后可以邀請用戶加入其帳戶,而這些用戶將需要在其記錄中設置account_id。 就目前而言,當我嘗試創建帳戶和所有者時,會收到以下服務器輸出和錯誤消息:(請注意,該帳戶不存在,也不對用戶進行劑量設置-它是一個干凈的數據庫。.我100%肯定由於我的人際關系建立方式而失敗。

(byebug) @account.valid?
  Account Exists (0.6ms)  SELECT  1 AS one FROM "accounts" WHERE LOWER("accounts"."subdomain") = LOWER($1) LIMIT $2  [["subdomain", "taurengroup"], ["LIMIT", 1]]
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "xxxxxx@xxxxxx.com"], ["LIMIT", 1]]
false
(byebug) @account.errors
#<ActiveModel::Errors:0x007faf15f91660 @base=#<Account id: nil, subdomain: "taurengroup", owner_id: nil, plan_id: nil, phone_verified: false, email_verified: false, account_active: false, created_at: nil, updated_at: nil>, @messages={:"owner.account"=>["must exist"]}, @details={:"owner.account"=>[{:error=>:blank}]}>

我的Account模型

class Account < ApplicationRecord
  RESTRICTED_SUBDOMAINS = %w(www admin loadlead)

  belongs_to :owner, class_name: 'User'
  has_many :users

  validates :owner, presence: true
  validates :subdomain, presence: true,
            uniqueness: { case_sensitive: false },
            format: { with: /\A[\w\-]+\Z/i, message: 'Contains invalid characters' },
            exclusion: { in: RESTRICTED_SUBDOMAINS, message: 'restricted name'}

  accepts_nested_attributes_for :owner

  before_validation :downcase_subdomain

private

  def downcase_subdomain
    self.subdomain = subdomain.try(:downcase)
  end

end

我的User模型

class User < ApplicationRecord
  enum role: [:system_admin, :owner, :admin, :user]
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

  validates :f_name, :l_name, :country_code, :phone_number, presence: true

  after_initialize :set_default_role, :if => :new_record?

  belongs_to :account
private

  def set_default_role
    self.role ||= :user
  end

end

我的AccountController創建方法:

  def create
    @account = Account.new(account_params)
    byebug
    if @account.valid?
      @account.owner.update_attributes(role: 1) 
      @account.owner.update_attributes(account_id: @account.id)
      @aaccount.save
      redirect_to root_path, notice: 'Company subdomain created successfully.'
    else
      render action: 'new', alert: 'There was a problem. Please try again.'
    end
  end

在這里的任何幫助將不勝感激。

嘗試創建時我的服務器輸出:

Started POST "/accounts" for 127.0.0.1 at 2018-04-09 21:21:05 -0600
Processing by AccountsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pH8vf0MtXpZ/rtyFw8dJ1XKeJ3heR9Mk8deDWWGDgbxrtgrtSuNvpmB2iVIs178z8g1AcSBrumm7DJCwqnoWEg==", "account"=>{"owner_attributes"=>{"f_name"=>"xxxxxx", "m_name"=>"xxxxxx", "l_name"=>"xxxxxx", "country_code"=>"1", "phone_number"=>"xxxxxxxx", "email"=>"xxxxxx@taurenltd.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "subdomain"=>"TaurenGroup"}, "commit"=>"Create Account"}

好的,所以我把這些都鈎了..它的工作..也許有更好的方法了???

User模型

belongs_to :account

Account模型

  has_one :owner, class_name: 'User'
  has_many :users

這樣做使我不必在創建時更新或創建關聯的屬性。

感謝您的輸入!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM