ruby on rails - Before_create not working -



ruby on rails - Before_create not working -

i have next model setup. can tell in logs, variable beingness saved in db null:

class bracket < activerecord::base before_create :set_round_to_one def set_round_to_one @round = 1 end end

i create using this:

bracket = bracket.new(:name => 'winners', :tournament_id => self.id) bracket.save

i did utilize create supposed new , save, didn't work either.

presuming round field in brackets table, need phone call setter:

self.round = 1

that's because round key bracket's attributes hash , calling setter value in hash set correctly.

further, @round = 1, you're causing new instance variable called round created first time called. , since activerecord not values in instance variables (it looks in attributes hash), nil happens far saving value of @round concerned.

ruby-on-rails ruby-on-rails-3

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -