【问题标题】:Rails transaction doesn't input correct dataRails 事务没有输入正确的数据
【发布时间】:2012-08-01 00:37:44
【问题描述】:

我的应用程序中的注册表单允许用户创建帐户 - 它还创建他们的用户记录和 accounts_users (has_many :through) 记录。一旦他们单击提交,我将调用“setup_account”方法为用户的新帐户输入几个默认值。话虽如此,该方法已接近工作,但为我手动分配值的三个记录输入了不正确的值(即:user_id => @user 或 :account_id => @account)。 account_id 和 user_id 的错误值始终为 1。有没有人知道为什么这不起作用?

这是我的帐户模型:

  def self.setup_account(p)
    Account.transaction do
      @account = Account.new(p)
      @account.save!
      @user = @account.users.first
      @user.create_profile!
      @group = @account.groups.create!( :user_id => @user.id, :name => 'Default' )
      @group.members.create!( :account_id => @account.id, :user_id => @user.id )
      Role.all.each do |role|
        @account.roles_users.create!( :user_id => @user.id, :role_id => role.id )
      end
    end
    return @account
  end

这是我的 AccountsController:

  def new
    @account = Account.new
    @user = @account.users.build()
    @account.accounts_users.build()
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @account }
    end
  end

  def create
    @account = Account.setup_account(params[:account])
    respond_to do |format|
      if !@account.nil?
        flash[:domain] = @account.subdomain
        format.html { redirect_to thanks_url }
        format.json { render json: @account, status: :created, location: @account }
      else
        format.html { render action: "new" }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

我的模特协会是:

  • 帐户(has_many :users;:through account_users; has_many :accounts_users; has_many :groups; has_many :members; has_many :roles_users)
  • 用户(has_one :profile; has_many :accounts_users; has_many :accounts, :through => :accounts_users; has_many :members; has_many :groups; has_many :roles_users; has_many :roles, :through => :roles_users)
  • Accounts_User(belongs_to :account;belongs_to :user)
  • 组(has_many :members,as:membership;belongs_to :user;belongs_to :account)
  • 成员(belongs_to :membership, :polymorphic => true;belongs_to :account;belongs_to :user)
  • 个人资料(属于:用户)
  • 角色(has_many :roles_users; has_many :users :through => :roles_users; has_many :accounts, :through => :roles_users)
  • RolesUser(belongs_to :user;belongs_to :account;belongs_to :role)

编辑:编辑 setup_account 方法并创建操作。

【问题讨论】:

    标签: ruby-on-rails-3 transactions


    【解决方案1】:

    在你的模型方法中检查我的 cmets,

    def self.setup_account(p)
      Account.transaction do
        @account = Account.new(p)
        # Account is not created yet so @account.users won't be retrieved properly, I guess
        # You need to save account first.
        @user = @account.users.first
        @user.build_profile
        @group = @account.groups.build( :user_id => @user, :name => 'Default' ) #Error
        @group.members.build( :account_id => @account, :user_id => @user ) #Error
        Role.all.each do |role| #Error
          @account.roles_users.build( :user_id => @user, :role_id => role.id )
        end
        # Try moving this above my comment.
        @account.save!
      end
    end
    

    由于您使用的是事务块,因此您可以在构建它们后立即开始保存它们。如果有任何错误,它会自动回滚。

    【讨论】:

    • 您最初的建议让我上路了,但这并不是我所需要的一切。经过一番摆弄,我得到了它的工作。您可以在我上面编辑的帖子中看到工作方法/操作。感谢您的帮助。
    • 你能再编辑一下,让方法在代码语法中正确格式化吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多