askegg wrote:
I am not sure I am following this or completely understanding how it’s
all stuck together.
Can you post the schema for these tables from you database?
The models might come in handy to…
Again thanks so much for your help.
You are exactly right on your assumptions about the layout of the 3
tables.
Here’s the declarations of the 3 models to show you how complex this
site is:
==== Account =====================================
class Account < ActiveRecord::Base
include ActionView::Helpers::NumberHelper
belongs_to :user
has_one :form_of_payment, :dependent => :destroy
has_one :account_plan, :dependent => :destroy
has_many :entries, :dependent => :delete_all, :order => ‘created_at,
id’
has_many :spaces, :foreign_key => “user_id”
has_many :payments,
:class_name => Entry,
:conditions => “code = ‘payment’”,
:order => ‘created_at, id’
has_many :charges,
:class_name => Entry,
:conditions => “code = ‘charge’”,
:order => ‘created_at, id’
has_many :credits,
:class_name => Entry,
:conditions => “code = ‘credit’”,
:order => ‘created_at, id’
has_many :debits,
:class_name => Entry,
:conditions => “code = ‘debit’”,
:order => ‘created_at, id’
==== Spaces ======================================
belongs_to :user
belongs_to :account, :foreign_key => “user_id” #******
belongs_to :owner, :class_name => ‘User’, :foreign_key => ‘owner_id’
belongs_to :home_page, :class_name => ‘WikiPage’, :foreign_key =>
‘home_page_id’
has_many :folders, :dependent => :destroy
has_many :folder_items, :dependent => :destroy
has_many :box_sets, :dependent => :destroy
has_many :boxes, :dependent => :destroy
has_one :root_folder
has_one :trash_folder
has_one :articles_folder
has_one :wiki_pages_folder
has_one :public_category, :class_name => ‘Category’, :conditions =>
“title = ‘Public’”
has_one :uncategorized_category, :class_name => ‘Category’,
:conditions => “title = ‘Uncategorized’”
has_many :pages, :dependent => :destroy
has_many :articles,
:dependent => :destroy,
:conditions => “type = ‘Article’”
has_many :wiki_pages,
:dependent => :destroy,
:conditions => “type = ‘WikiPage’”
has_many :dailies, :dependent => :delete_all
has_many :categories, :dependent => :destroy
has_many :space_resources,
:dependent => :destroy,
:conditions => “type = ‘SpaceResource’”
has_many :attachments,
:dependent => :destroy,
:conditions => “type = ‘Attachment’”
has_many :space_files,
:dependent => :destroy,
:conditions => “type = ‘SpaceFile’”
has_many :searches, :dependent => :delete_all
has_many :page_semaphores, :dependent => :delete_all
has_many :memberships, :dependent => :destroy
has_many :tabs, :order => ‘position asc’, :dependent => :delete_all
has_many :preferences, :dependent => :delete_all
has_many :invites, :dependent => :delete_all
has_many :comments, :dependent => :destroy
has_many :trackbacks, :dependent => :destroy
has_many :events, :dependent => :destroy
has_many :activities, :dependent => :delete_all
has_many :bookmarks, :dependent => :delete_all
has_many :members, :class_name => ‘User’, :through => :memberships,
:source => :user
has_one :calendar, :dependent => :destroy
has_one :blog_roll, :dependent => :destroy
has_space_resource :logo_resource
has_space_resource :banner_resource
has_space_resource :stylesheet_resource
has_space_resource :favicon_resource
has_one :anonymous_membership, :class_name => ‘Membership’,
:conditions => ‘memberships.user_id = #{User.anonymous_user.id}’
has_one :prototype_membership, :class_name => ‘Membership’,
:conditions => ‘memberships.user_id = #{User.prototype_user.id}’
==== users =======================================
belongs_to :last_space, :class_name => ‘Space’, :foreign_key =>
‘last_space_id’
has_one :account, :dependent => :destroy
has_many :spaces, :dependent => :destroy
has_many :owned_spaces, :class_name => ‘Space’, :foreign_key =>
‘owner_id’, :conditions => ‘closed_on IS NULL’
has_many :pages, :dependent => :destroy
has_many :comments, :dependent => :delete_all
has_many :resources, :dependent => :destroy
has_many :events, :dependent => :destroy
has_many :memberships, :dependent => :destroy
has_many :preferences, :dependent => :delete_all
has_many :page_semaphores, :dependent => :delete_all
has_many :activities
This gives you an idea of why my mind is blown. One questionable line
of code in the User class model is this:
has_one :account, :dependent => :destroy
has_many :spaces, :dependent => :destroy
I don’t know how this could effect the joining of the tables.
Argh!!!
Your examples seem to suggest the following (psudeo code):
Account Table—
id, :int
name, :string
…
end
Spaces Table –
id, :int
name, :string
account_id, :int
end
By specifying the foreign key the way you have, changes the spaces
table to:
Spaces Table –
id, :int
name, :string
user_id, :int
end
BUT, your mention of user_id leads me to suspect the following:
Account Table—
id, :int
name, :string
user_id, :int
…
end
Spaces Table –
id, :int
name, :string
user_id, :int
end
UserTable—
id, :int
name, :string
…
end
In which case the models would actually be:
class Account < ActiveRecord::Base
belongs_to :user
has_many :spaces, :through => :user
end
class Space < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :accounts
has_many :spaces
end
UNLESS “accounts” really means “users” and the tables/class name are
actually different.
See why we need the models and schema?