Hi, all!
Maybe a noob question, sorry, but I’m really stuck with this.
I have 2 models with has_many and belongs_to associations.
class Transaction < ActiveRecord::Base
has_many :bonuses, :class_name => ‘TransactionBonus’
end
class TransactionBonus < ActiveRecord::Base
belongs_to :transaction
end
The problem is that I cannot save TransactionBonus record to the
database - it’s simply doesn’t do anything!
I have another models in my application, and all of them are saved
just fine
TransactionBonus is the only exception
Table structure matches exactly the model records:
CREATE TABLE IF NOT EXISTS transaction_bonuses
(
id
int(11) NOT NULL auto_increment,
transaction_id
int(11) NOT NULL,
taken_bonus
float default NULL,
collected_bonus
float default NULL,
final_sum
float default NULL,
created_at
datetime default NULL,
updated_at
datetime default NULL,
PRIMARY KEY (id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Here is my irb console output with test records:
transaction=Transaction.find(1)
=> #<Transaction id: 1, card_account_id: 1, created_at:
“2008-09-01 13:48:10”, updated_at: “2008-09-01 13:48:10”>
transaction.bonuses
=> []
bonus=transaction.bonuses.create(:transaction_id => 1, :final_sum => 99)
=> #<TransactionBonus id: nil, transaction_id: 1, final_sum: 99.0,
created_at: nil, updated_at:
nil>
transaction.bonuses
=> [#<TransactionBonus id: nil, transaction_id: 1, final_sum: 99.0,
created_at: nil, updated_at:
nil>]
bonus.save
=> #<Transaction id: 1, card_account_id: 1, created_at:
“2008-09-01 13:48:10”, updated_at: “2008-09-01 13:48:10”>
bonus
=> #<TransactionBonus id: nil, transaction_id: 1, final_sum: 99.0,
created_at: nil, updated_at:
nil>
After that I have tried to initialize TransactionBonus record
directly, but no success:
bonus = TransactionBonus.create( :transaction_id => 1 )
=> #<TransactionBonus id: nil, transaction_id: 1, final_sum: nil,
created_at: nil, updated_at: nil>
bonus
=> #<TransactionBonus id: nil, transaction_id: 1, final_sum: nil,
created_at: nil, updated_at: nil>
bonus.save
=> #<Transaction id: 1, card_holder_id: 0, card_account_id: 1,
created_at: “2008-09-01 13:48:10”, updated_at: “2008-09-01
13:48:10”>
bonus
=> #<TransactionBonus id: nil, transaction_id: 1, final_sum: nil,
created_at: nil, updated_at: nil>
Please, note id: nil for the bonus var, it means that the record is
not saved to the database.
This is really mysterious for me…