Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过查找或创建_Ruby On Rails_Postgresql_Activerecord - Fatal编程技术网

Ruby on rails 通过查找或创建

Ruby on rails 通过查找或创建,ruby-on-rails,postgresql,activerecord,Ruby On Rails,Postgresql,Activerecord,我已经在我的模型中添加了以下setter/getter方法,尽管每当我尝试保存表单时,我都会遇到一个关于批量分配的错误。根据我的理解,如果找不到对手的名字,它会在数据库中添加一个条目 def opponent_name opponent.try(:name) end def opponent_name(name) self.opponent = Opponent.find_or_create_by_name(name) if name.present? end 下面是控制台日志中

我已经在我的模型中添加了以下setter/getter方法,尽管每当我尝试保存表单时,我都会遇到一个关于批量分配的错误。根据我的理解,如果找不到对手的名字,它会在数据库中添加一个条目

def opponent_name
opponent.try(:name)
end

def opponent_name(name)
    self.opponent = Opponent.find_or_create_by_name(name) if name.present?
  end
下面是控制台日志中的错误

    Started POST "/events" for 127.0.0.1 at 2013-03-26 19:07:26 +1100
Processing by EventsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"h7OrLKeDL/9KmZeGZeO+QTWHtlUdOlaMqnoMGhYaDUU=", "event"=>{"datetime(3i)"=>"2", "datetime(2i)"=>"3", "datetime(1i)"=>"2013", "datetime(4i)"=>"00", "datetime(5i)"=>"00", "event"=>"1", "location_id"=>"7", "duration"=>"30", "arrival_time"=>"30", "opponent_name"=>"Test", "home_or_away"=>"Home"}, "commit"=>"Create Event"}
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
Completed 500 Internal Server Error in 14ms

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: opponent_name):
  app/controllers/application_controller.rb:22:in `catch_not_found'
对手模型

class Opponent < ActiveRecord::Base
  has_many  :events
  belongs_to  :team

  attr_accessible :name, :team_id

  validates :name, :presence => true
end
attr_accessible :opponent_name
classtrue
结束
事件模型

class Event < ActiveRecord::Base
  include PublicActivity::Model
  tracked
  belongs_to :location
  belongs_to :opponent
  belongs_to :team
  belongs_to :result
  has_many :availabilities, :dependent => :destroy

  def opponent_name
    opponent.try(:name)
  end

  def opponent_name(name)
    self.opponent = Opponent.find_or_create_by_name(name) if name.present?
  end

  attr_accessible :location_id, :user_id, :datetime, :score_for, :score_against, :event,
                  :team_id, :home_or_away, :arrival_time, :duration, :selected_players, :date, :time, :result_id

  validates :event, :location_id, :team_id, :presence => true
  validates_numericality_of :team_id, :only_integer => true, :greater_than_or_equal_to =>0, :message => " needs to be set, please contact your Administrator"
  #validates_numericality_of :arrival_time, :only_integer =>true, :greater_than_or_equal_to =>0, :message => " must be greater than 0 minutes", :allow_blank => true
  validates :home_or_away, :presence => true, :if => :event == 1
  validates :score_for, :presence => true, :if => :score_against
  validates :score_against, :presence => true, :if => :score_for

  EVENT_TYPES = [['Game', 1], ['Training', 2], ['Social Event', 3]]
  HOME_OR_AWAY = [:Home, :Away]

end
class事件:销毁
def对手名称
对手。试试(:name)
结束
def对手名称(名称)
self.hatcher=对手。如果name.present,则通过名称(name)查找或创建?
结束
属性可访问:位置id、:用户id、:日期时间、:分数针对、:分数针对、:事件、,
:球队id、:主场或客场、:到达时间、:持续时间、:选定球员、:日期、:时间、:结果id
验证:事件、:位置\u id、:团队\u id、:状态=>true
验证:team\u id、:only\u integer=>true、:大于\u或等于\u to=>0的\u数值性;:message=>“需要设置,请与管理员联系”
#验证以下各项的数值性:到达时间、仅整数=>真、大于或等于0、消息=>必须大于0分钟、允许空白=>真
验证:home\u或\u away,:presence=>true,:if=>:event==1
验证:score\u for,:presence=>true,:if=>:score\u for
验证:score\u针对,:presence=>true,:if=>:score\u针对
事件类型=[['Game',1],'Training',2],'Social EVENT',3]]
HOME\u或_AWAY=[:HOME,:AWAY]
结束
看一看

我相信你必须在你的对手模型中添加以下语句

class Opponent < ActiveRecord::Base
  has_many  :events
  belongs_to  :team

  attr_accessible :name, :team_id

  validates :name, :presence => true
end
attr_accessible :opponent_name

试着加入你的事件模型

attr_accessible :opponent_name
它应该会清除错误

编辑:

只是更新一个答案,但所有学分都将用于此编辑


问题可能是您像def一样定义了setter 对手名称(name),而它应该是def对手名称=(name)


如果名称是模型数据库表中的一个字段,那么Rails已经为该属性定义了getter和setter。您只需添加 属性可访问:对手名称

Reference@misha


问题可能是您将二传手定义为def opporter_name(name),而它应该是def opporter_name=(name)。当您执行此操作并且attr_accessible:对手名称时,它可能会起作用。不知道为什么会在那一行出错。似乎与错误无关。

模型的其余部分是什么样子的?您是否已使用attr\u accessible将必要的列(包括对手名称)列入白名单?
attr\u accessible:location\u id、:user\u id、:datetime、:score\u for、:score\u from、:event、:team\u id、:home\u or\u offer、:arrival\u time、:duration、:selected\u players、:date、:time、,:result\u id
当我添加:对手名称时,我得到
ActiveRecord::UnknownAttributeError(未知属性:对手名称):
问题可能是您将二传手定义为
def对手名称(名称)
,而它应该是
def对手名称=(名称)
。当您执行此操作并
attr\u accessible:opporter\u name
时,它可能会起作用。不知道为什么会在那一行出错。似乎与错误无关。我会给他+2,但我不能给他,因为它只允许+1,因为他看到了明显的我们都忽略了的东西:)@Paul'Whippet'McGuane还尝试在属性、验证、关联和其他一般定义之后定义您的方法。它使代码更具可读性,并且在将来更容易跟踪问题。只是提醒一下。
ActiveRecord::事件控制器中的未知属性错误#创建未知属性:对手名称
,然后您从对手模型中删除了
attr\u accessible:对手名称
?只要仔细检查一下就可以了,我不需要在我的数据库中有一个名为“对手名称”的列,让attar\u可以工作/不需要在数据库中有一个列,让attr\u可以工作。如果在表单中定义它,则可以在模型中作为getter和setter访问它,但这是另一回事。第二个问题-在所有属性和其他元素之后,您是否尝试过在模型的最后定义
def opporter\u name