Hello!
I have a somewhat puzzling problem. I have running code on a server, and
on a colleagues computer, but the same code does not run on my computer.
Some model objects just dont seem to want to instantiate, Instead i get
this error when i run for example:
>> MetaFile.new
NoMethodError: You have a nil object when you didn''t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/base.rb:1114:in
`id''
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations/belongs_to_association.rb:67:in
`construct_sql''
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations/belongs_to_association.rb:7:in
`initialize''
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations.rb:654:in
`data_holder=''
from
/usr/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations.rb:651:in
`data_holder=''
from ./script/../config/..//app/models/meta_file.rb:23:in
`initialize''
from (irb):1
This works perfectly on my friends computer, and we are both running
Linux Ubuntu breezy. I have tried running this in irb, webrick and
lighttpd, with the same error.
I am at a loss as to what the problem is, im guessing some difference in
the postgresql adapter, but there shouldnt be any! I can create some
objects though, for example a User in my system. The sql for these looks
like this:
The model that works:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(80) NOT NULL,
password VARCHAR(40) NOT NULL,
email VARCHAR(60),
firstname VARCHAR(40),
lastname VARCHAR(40),
inactive BOOLEAN default false,
primary_group INT references groups,
signature text,
meta_file_id INT,
description TEXT,
notify_emails BOOLEAN default false,
custom_1 TEXT,
custom_2 TEXT,
custom_3 TEXT,
locale VARCHAR(10)
);
And one that doesnt:
CREATE TABLE meta_files (
id SERIAL PRIMARY KEY,
type VARCHAR(20) NOT NULL,
name VARCHAR(80) NOT NULL,
file_type VARCHAR(80) NOT NULL,
size INT,
access boolean,
description TEXT,
updated_on TIMESTAMP NOT NULL,
created_on TIMESTAMP NOT NULL,
user_id INT references users,
data_holder_id INT references data_holders ON DELETE CASCADE ON
UPDATE CASCADE
);
And the models looks like this:
User:
class User < ActiveRecord::Base
make_searchable [ :username,:description, :firstname, :lastname ]
### ASSOCIATION SECTION
has_and_belongs_to_many :groups
belongs_to :primary_group, :foreign_key => "primary_group",
:class_name => "Group"
has_one :profile_image, :dependent=>true
has_many :chats, :dependent=>true
has_many :received_messages, :dependent => true
# shared contents
has_many :meta_files, :dependent=>true
has_many :data_holders, :dependent => true, :foreign_key =>
''created_by''
has_many :documents, :dependent => true, :foreign_key =>
''created_by_user''
has_many :messages, :dependent => true
has_many :active_documents, :dependent => true
has_many :active_files, :dependent => true
has_many :temporary_contents, :dependent => true
has_many :active_contents, :dependent => true
has_many :shortcuts, :class_name => ''ActiveShortcut'',
:dependent => true
### VALIDATION SECTION
# add validation for primary group
# the user must have choosen one group
validates_uniqueness_of :username
validates_length_of :username, :within => 3..40
validates_length_of :password, :within => 5..40
validates_presence_of :username, :password, :groups
validates_confirmation_of :password
And the one that cant be instantiated:
class MetaFile < ActiveRecord::Base
make_searchable [ :name, :description, :extra_index_fields]
belongs_to :user
belongs_to :data_holder
has_many :active_files, :dependent => true
attr_accessor :tmp_file
MAX_FILE_SIZE = 10000000
after_destroy :remove_data_holder, :remove_file
#
# DataHolder integration
#
def initialize(*args)
self.data_holder = DataHolder.new(:data_type => DataHolder::FILE)
super
end
Now i know this is a lot of data, so I dont expect anyone to actually
read it :) But if anyone has had a similar problem, it would be nice to
get some hints as to what could be wrong. Oh, and i know it isnt the
initialize in the MetaFile class....