ruby-on-rails-3 - 导轨 : Uniqueness of two attributes in join table causing 500 error

标签 ruby-on-rails-3 model jointable validates-uniqueness-of

我有以下模型,它们基本上是试图表示 教授知识许多 主题 对于特定的 级别 .科目是固定的,因此不会创建新科目,只会通过知识连接表与教授“相关”。

class Subject < ActiveRecord::Base
  # Self Associations
  has_many :subcategories, :class_name => "Subject"
  belongs_to :category, :class_name => "Subject",:foreign_key => "parent_id"

  # Associations
  has_many :knowledges
  has_many :professors, :through => :knowledges
end


class Professor < ActiveRecord::Base
  # Associations
  has_many :knowledges
  has_many :subjects, :through => :knowledges
  ...
end

class Knowledge < ActiveRecord::Base
  # Associations
  belongs_to :professor
  belongs_to :subject
  has_one :level

  attr_accessible :subject_id, :professor_id

  validates :subject_id, :uniqueness => { :scope => :professor_id }
end

我想要一个表格,让教授可以在他的帐户中添加一个主题,我决定有一个知识表格(因为我也希望能够插入一个级别)。

它看起来像这样:
<%= simple_form_for @knowledge,:url => professor_knowledges_path, :html => { :class => 'form-horizontal' } do |f| %>
    <div class="control-group select optional">
      <%= label_tag "Subject Type", nil, :class => "select optional control-label"%>
      <div class="controls">
    <%= select_tag "Parent Subject", options_from_collection_for_select(@parent_subjects, "id", "name"), :id => "knowledge_parent_subject" %>
      </div>
    </div>
    <%= f.input :subject_id, :collection => @subjects, :label => "Subject" %>
    <%= f.input :level %>
  <%= f.button :submit, t('add_form'),:class => 'btn-primary' %>
<% end %>

而在 创建 知识 Controller 的 Action 我有这个:
def create
    @knowledge = Knowledge.create(:professor_id => current_professor.id, :subject_id => params[:knowledge][:subject_id]) 
  end

我想/期望得到一个 ActiveRecord 说这个知识不能被插入,因为存在唯一性冲突,但是不,我只是在日志中看到一个 500 和一个回滚,但似乎执行仍在继续。所以我的问题是:我做错了什么,或者我如何改进这种建模情况?我相信表单需要与连接模型相关,因为我想在它上面有那个模型的字段......但也许我错了,我可以用一种更简单/更干净的方式来做。

编辑 :

正如评论之一所问,这里是提交表单的日志和回滚后的 500 错误:
Started POST "/professors/1/knowledges" for 127.0.0.1 at 2012-07-01 00:45:39 -0700
Processing by KnowledgesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"4JVyxWnIh37kyBwLwLGTHk/znsI1c5wrJvaWjKKT5tM=", "Parent Subject"=>"1", "knowledge"=>{"subject_id"=>"1"}, "commit"=>"Añadir", "professor_id"=>"1"}
  Professor Load (0.4ms)  SELECT `professors`.* FROM `professors` WHERE `professors`.`id` = 1 LIMIT 1
Completed 500 Internal Server Error in 4ms

我在中添加了一些条件创建 行动,像这样:
  def create
    @knowledge = Knowledge.new(:professor_id => current_professor.id, :subject_id => params[:knowledge][:subject_id]) 
    if @knowledge.save
      flash[:notice] = "Success..."
      redirect_to professor_path(current_professor)
    else
      render :action => 'new'
    end
  end

这实际上在 500 之后显示了以下内容:
Completed 500 Internal Server Error in 6ms

ActiveRecord::RecordInvalid (Validation failed: Subject has already been taken):

我想知道为什么会引发异常而不是将错误添加到对象中并让我管理这种情况。下面这行不是应该做的吗?
validates :subject_id, :uniqueness => { :scope => :professor_id }

最佳答案

我没有足够的声誉来发表评论......我的答案更多的是尝试一些事情而不是一个明确的答案,抱歉。

由于验证错误,保存似乎失败。您可以尝试处理“else”块中的那些。下面将向您介绍所有验证错误(对调试很有用)。

@knowledge.errors.full_messages

您尚未显示"new"操作中发生的情况。我怀疑这是发生错误的地方。

控制台中是否出现同样的问题(即验证问题)?如果是这样,请尝试清理您的数据库(请注意 - 以下内容将删除并重建您的所有数据库)。

rake db:drop:all db:create:all db:migrate db:test:prepare



此外,如果您还没有,请为 Knowledge 的迁移添加一个索引,以防止将重复项添加到数据库中。例如
    add_index :knowledges, [ :professor_id, :subject_id ], unique: true

关于ruby-on-rails-3 - 导轨 : Uniqueness of two attributes in join table causing 500 error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11271702/

相关文章:

ruby-on-rails - Rails 服务器启动时如何运行 "rake resque:work QUEUE=*"?

ruby - Rails 3.1.0 使用 haml 的问题 - 缺少模板错误

sql - 将三个表连接成一个连接表

iphone - iOS中如何获取设备型号?

java - 删除多行卡住 JTable

mysql - 改变表添加外键引用

mysql - 如何连接多个表,包括查找表并按行返回数据

ruby-on-rails - 你什么时候将你的应用升级到 Rails 3?

ruby-on-rails - Heroku托管的Rails应用的最佳搜索选项?

php - Laravel 存储库和 Eloquent 模型