2013-03-26 63 views
1

我已將以下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 
+0

你的模型的其餘部分是什麼樣的?您是否使用attr_accessible列出了必要的列(包括opponent_name)? – pdoherty926 2013-03-26 08:16:27

+0

'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 – 2013-03-26 09:20:28

+0

當我添加:opponent_name我得到'ActiveRecord :: UnknownAttributeError(未知屬性:opponent_name):' – 2013-03-26 09:22:26

回答

1

嘗試把你的事件模型

attr_accessible :opponent_name 

應該清除,則錯誤

編輯:

只需更新一個答案,但所有學分都會進入Mischa進行編輯。

的問題可能是你定義你的二傳手喜歡高清 OPPONENT_NAME(名稱),而應該高清OPPONENT_NAME =(名稱)

+0

@ Paul'Whippet'McGuane還嘗試在attr_accessible,驗證,關聯和其他通用定義之後定義您的方法。它使代碼更具可讀性,並且更容易在將來追蹤問題。只是和建議。 – Aleks 2013-03-26 10:50:37

+0

EventsController中的ActiveRecord :: UnknownAttributeError#create 未知屬性:opponent_name' – 2013-03-26 11:03:02

+0

並且您從對手模型中刪除了'attr_accessible:opponent_name'?只是雙重檢查 – Aleks 2013-03-26 11:19:43

0

如果OPPONENT_NAME是在模型的數據庫表中的字段,然後Rails已經爲該屬性定義了getter和setter。所有你需要做的就是添加 attr_accessible:OPPONENT_NAME

0

參考@Mischa

的問題可能是你定義你的二傳手喜歡高清OPPONENT_NAME(名稱),而應該是高清OPPONENT_NAME =(名稱)。當你這樣做和attr_accessible:opponent_name,它可能會工作。不知道爲什麼它在這條線上出錯。似乎與錯誤無關。