使用'has_many:through'记录关联更新记录时出现问题

时间:2011-07-05 19:57:41

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

我正在使用Ruby on Rails 3.0.7,我对更新控制器操作的关联模型验证存在问题。我正在使用“has_many:through”关联,以便我可以处理用户类别关系。

User模型中我有:

has_many :user_category_relationships,
  :autosave    => true,
  :dependent   => :destroy
has_many :user_categories,
  :through     => :user_category_relationships,
  :source      => :user_category,
  :dependent   => :destroy

_form视图文件中我有:

<% @current_user.user_categories.each do |user_category| %>
  <%= check_box_tag :user_category_ids, user_category.id, @user.user_categories.include?(user_category), :name => 'user_relationships[user_category_ids][]' %>
  <%= label_tag "user_category_#{user_category.id}", user_category.name %>
<% end %>

UserCategoryRelationship模型中我有:

belongs_to :users
belongs_to :categories

# Validation
validates :name_id,
  :presence   => true,
  :uniqueness => {
    :scope          => [:created_by_user_id, :category_id]
  }
validates :category_id,
  :presence   => true,
  :uniqueness => {
    :scope          => [:created_by_user_id, :name_id]
  }
validates :created_by_user_id,
  :presence   => true,
  :uniqueness => {
    :scope          => [:category_id, :name_id]
  }

UsersController我有:

def create
  ...

  # `params[:user_relationships][:user_category_ids]` used below are id values 
  # related to user-category relationships.

  unless params[:user_relationships][:user_category_ids]
    # Set default user category if not selected.
    @user.user_category_relationships.build(
      :category_id        => '1',
      :created_by_user_id => @current_user.id,
      :name_id            => @name.id
    )
  else
    params[:user_relationships][:user_category_ids].each { |user_category_id|
      @user.user_category_relationships.build(
        :category_id        => user_category_id,
        :created_by_user_id => @current_user.id,
        :name_id            => @name.id
      )
    }
  end

  if @user.save
    ...
  end
end

def update
  ...

  # Note: The following code is equal to that in the controller 'create' 
  # action. However here is my problem because validation (read after 
  # for more information about).

  unless params[:user_relationships][:user_category_ids]
    # Set default user category if not selected.
    @user.user_category_relationships.build(
      :category_id        => '1',
      :created_by_user_id => @current_user.id,
      :name_id            => @name.id
    )
  else
    params[:user_relationships][:user_category_ids].each { |user_category_id|
      @user.user_category_relationships.build(
        :category_id        => user_category_id,
        :created_by_user_id => @current_user.id,
        :name_id            => @name.id
      )
    }
  end

  if @user.update_attributes(params[:user])
    ...
  end
end

上述代码除update操作外有效:当我尝试运行时,我总是得到同样的错误,以及

# Validation errors on updating the 'user_category_relationships' associated model
:"user_category_relationships.category_id"        =>["has already been taken"]
:"user_category_relationships.name_id"            =>["has already been taken"]
:"user_category_relationships.created_by_user_id" =>["has already been taken"]

我想做什么,因为我根本没有使用“RoR魔法\自动方式”(也就是说,我没有使用collection_singular_ids方法 - 请参阅还thisthis),是为了正确使用update控制器操作,这样我仍然可以使用“自动”创建用户类别关系 (在saveupdate_attributes(...)方法上触发)但避免生成上述验证错误(即,在数据库表中创建有效数据)。 我该怎么办?你对这个问题有什么建议?

PS:我遇到麻烦的地方是update行动,准确地弄清楚如何“选择”“过滤”和“编码”记录来更新,创建和删除具有参数的数据库[:user_relationships] [:user_category_ids]作为输入数据。

0 个答案:

没有答案