I have a few small ROR "mostly static" type sites and am working on something a little deeper that I don't have much experience with...
The BIG picture nomenclature. Accounts have many users and projects. users have many projects. users add many files and notes to projects....
In the end I need to generate a view where users can see their projects, files, & notes. In a list or table similar to the MANUALLY created "proof of concept" one below:
Project1
Notes
note1
note2
note3
Files
file1
file2
file2
Users
user1
开发者_如何学C user2
Project2
Notes
note1
note2
note3
Files
file1
file2
file2
Users
user1
user2
The list above will be generated using some sort of an embedded ruby for loop but before I get into that I need to ensure I have the proper model associations and calls.
I'm attempting to have NO foreign keys from all my tables but I've been getting really confused with multi-model best practices. With no foreign keys I THINK I need a join table, which could be a model using "model1_model2" naming convention?, and the ":through" model relationship?
my models now (this has been changing alot) are:
Account:
class Account < ActiveRecord::Base
has_many :projects
has_many :users
end
User:
class User < ActiveRecord::Base
has_one :account
has_many :projects, :through => :accounts
has_many :dataFiles, :through => :projects
has_many :notes, :through => :projects
end
Project:
class Project < ActiveRecord::Base
belongs_to :account
has_many :users
has_many :datafiles
has_many :notes
end
DataFile:
class DataFile < ActiveRecord::Base
belongs_to :projects
belongs_to :users
end
Note:
class Note < ActiveRecord::Base
belongs_to :project
belongs_to :users
end
As you can probably see; I'm confused here! I've done a bunch of tutorials and read a book; this is my first real world application that isn't just mainly static pages....
There appears to be a lot of ways this could be done. I guess I'm looking for some expert direction on what models I should use and how I should connect them.
Any direction or advice you can offer is greatly appreciated. Thank you!
If i understood you correctly
class Accounts < ActiveRecord::Base
# these two associations say an Account can have many users.
# It's also assuming users can be associated with multiple accounts. If that's false
# i'd recommend putting the account_id on the user and simply removing this many-to-many table
has_many :account_users
has_many :users, :through => :account_users
# accounts can be mapped to many projects and projects can be mapped to many accounts
# if a project only belongs to one account, drop the accounts_projects many-to-many table
# and just put the account_id on the project.
has_many :account_projects
has_many :projects, :through => :account_projects
end
# account_user table will have `id`, `account_id`, `user_id` and anything else you need
class AccountUser < ActiveRecord::Base
belongs_to :account
belongs_to :user
end
class User < ActiveRecord::Base
has_many :projects
has_many :files
end
# account_projects table will have `id`, `account_id`, `project_id` and anything else you need
class AccountProject < ActiveRecord::Base
belongs_to :account
belongs_to :project
end
class Project < ActiveRecord::Base
has_many :data_files
has_many :notes
has_many :project_users
has_many :users
end
# project_users table will have `id`, `project_id`, `user_id` and anything else you need
class ProjectUser < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
# data_files table will have `id`, `project_id`, `user_id` and anything else you need
class DataFile < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
# notes table will have `id`, `project_id`, `user_id` and anything else you need
class Note < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
Does that help explain how the associations will work in rails?
NOTE The AccountUser, AccountProject and ProjectUser tables are all used for the many to many associations I understood you to need.
The through associations are useful if you're ever going to associate any other attributes with the mapping of the associations (something in relation to both the account and user for example).
If you just need a simple many-to-many relationship without the need for custom attributes, you can simply use the has_and_belongs_to_many
approach, though I usually go for the :through option up front.
The no carriage return comment field irritates me...
Users can only belong to one account (except for admins) so I think I will take your suggestion and remove the many to many table and use an account_id field in the users table. Same for the accounts_projects many to many...
it sounds like the best way to associate all these models is "belongs_to" and "has_many" with foreign keys stored in the appropriate tables. I appreciate the eduction you've given and the real world insight on associated all these models. Thanks! hopefully I don't encounter any issues with setting this all up.
Thanks!
精彩评论