找到或创建

时间:2013-03-26 08:11:12

标签: ruby-on-rails postgresql activerecord

我已经将以下的setter / getter方法添加到我的模型中,但每当我尝试保存表单时,我都会收到有关质量分配的错误。从我的理解这应该如何工作,如果找不到opponent_name它将添加一个条目到数据库

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

事件模型

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

4 个答案:

答案 0 :(得分:2)

查看ActiveModel::MassAssignmentSecurity::ClassMethods

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

attr_accessible :opponent_name

答案 1 :(得分:1)

尝试加入事件模型

attr_accessible :opponent_name

应该清除错误

修改

只需更新答案,但所有信用均转到Mischa进行此修改。

  

问题可能是您定义了像def这样的setter   opponent_name(name),而它应该是def opponent_name =(name)

答案 2 :(得分:0)

如果opponent_name是模型数据库表中的一个字段,那么Rails已经为该属性定义了getter和setter。您需要做的就是添加 attr_accessible:opponent_name

答案 3 :(得分:0)

参考@Mischa

问题可能是您定义了像def opponent_name(name)这样的setter,而它应该是def opponent_name =(name)。当你这样做和attr_accessible:opponent_name,它可能会工作。不知道为什么它在该行上出错。似乎与错误无关。