rails - what is the best/most efficient way to associate these models
In my application I have the following situation:
User belongs_to Group Group has_many Users
Project belongs_to User User has_many projects
The following is also true: - Each Group will have one BaseCase - Each Project will have multiple Scenarios and one BaseCase(depending on the开发者_高级运维 Group the Project User belongs to) - Scenario and BaseCase is the same type of object (let's call this Data) - The default values for each Scenario are the BaseCase values for the Group, but the User can change these default values to create the specific Scenario
I am not sure how to capture all these relationships through associations efficiently, does anyone have any ideas? thanks
If I understand correctly, then something like that
class User
belongs_to :group
has_many :projects
end
class Group
has_many :users
has_many :projects, :through => :users
has_one :base_case
end
class Project
has_many :scenarios
has_one :base_case
belongs_to :user
has_one :group, :through => user
has_one :base_case, :through => :group
end
class Scenario
belongs_to :project
has_one :base_case, :through => :project
before_create do
self.attributes = self.base_case.attributes.except(:group_id, ...)
end
end
class BaseCase
belongs_to :group
end
精彩评论