如何更新 RoR 模型的关联而不更新相应模型的属性

发布于 2024-11-01 00:23:56 字数 1015 浏览 2 评论 0原文

我的 Rails 3 应用程序中有以下模型(带有相应的数据库表):

class User < ActiveRecord::Base
  has_many :service_users
  has_many :services, :through => :service_users

  attr_accessor :password

  attr_accessible :password, :password_confirmation, :service_ids

  validates :password, :presence => true,
                       :confirmation => true,
                       :length => { :within => 6..40 }
  ...
end

class Service < ActiveRecord::Base
  has_many :service_users
  has_many :users, :through => :service_users
  ...
end


class ServiceUser < ActiveRecord::Base
  belongs_to :service
  belongs_to :user
end

#User Controller:

class UsersController < ApplicationController
  ...
  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated."
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
    end
  end
  ...
end

我希望能够更新用户模型的关联服务,而不必指定密码和密码确认属性。我该怎么做?

I have the following models (with corresponding database tables) in my Rails 3 application:

class User < ActiveRecord::Base
  has_many :service_users
  has_many :services, :through => :service_users

  attr_accessor :password

  attr_accessible :password, :password_confirmation, :service_ids

  validates :password, :presence => true,
                       :confirmation => true,
                       :length => { :within => 6..40 }
  ...
end

class Service < ActiveRecord::Base
  has_many :service_users
  has_many :users, :through => :service_users
  ...
end


class ServiceUser < ActiveRecord::Base
  belongs_to :service
  belongs_to :user
end

#User Controller:

class UsersController < ApplicationController
  ...
  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated."
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
    end
  end
  ...
end

I want to be able to update a User model's associated services without having to specify the password and password confirmation attributes. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半衾梦 2024-11-08 00:23:56

两个选项...

如果您有简单的逻辑,例如仅在创建用户时验证密码,这将起作用:

validates :password, :presence => true,
                     :confirmation => true,
                     :length => { :within => 6..40 },
                     :if => :new_record?

更有可能的是,您需要一个组合,以便用户可以更新其密码:

validates :password, :presence => true,
                     :confirmation => true,
                     :length => { :within => 6..40 },
                     :if => :is_password_validation_needed?

# Protect the password attribute from writing an
# empty or nil value
def password=(pass)
  return if !pass.present?
  @password = pass
end

private 

  def is_password_validation_needed?
    # Return true if the record is unsaved or there
    # is a non-nil value in self.password
    new_record? || password
  end

Two options...

If you have simple logic, like only validating the password when a user is created, this will work:

validates :password, :presence => true,
                     :confirmation => true,
                     :length => { :within => 6..40 },
                     :if => :new_record?

More likely, you'll want a combination so that users can update their password:

validates :password, :presence => true,
                     :confirmation => true,
                     :length => { :within => 6..40 },
                     :if => :is_password_validation_needed?

# Protect the password attribute from writing an
# empty or nil value
def password=(pass)
  return if !pass.present?
  @password = pass
end

private 

  def is_password_validation_needed?
    # Return true if the record is unsaved or there
    # is a non-nil value in self.password
    new_record? || password
  end
爱要勇敢去追 2024-11-08 00:23:56

您将需要研究条件验证,以指定在更新/保存模型时是否应验证模型中的密码属性。这是一个 Railscast 剧集,虽然有点过时,但仍然相当简单,应该得到你走在正确的道路上

you will want to look into conditional validation to specify whether or not the password attribute in your model should be validated when you are updating/saving the model. here is a Railscast episode that, while a little dated, is still fairly straightforward and should get you off on the right path

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文