English 中文(简体)
RoR数据库计划和协会咨询
原标题:Advice on RoR database schema and associations

我在找一些关于我DB结构的建议。

我有4个模型/表格:用户 & gt; 客户端 & gt; 任务 & gt; 任务

该应用程序将通过以下方式运作:

  1. Users will login
  2. They can add Clients
  3. They can add Jobs to Clients
  4. They can add Tasks to Jobs

因此,任务属于工作,工作属于客户,客户属于用户。

我想为客户端、 工作或任务中的任何对象查询 DB, 并且确保它属于当前登录用户。 我正努力写一个轨迹连接查询, 并设计我的协会, 这样我才能做到这一点 。

我知道,如果我在每个桌子上都有一个用户 id 字段,这将是非常容易的,但是,这似乎不是这样做的正确方法。

我在http://guides.rubyonrails.org/association_basics.html 上读过指南,但是仍然在黑暗中。有谁能说明一下我如何构建我的DB,更重要的是我的协会?

xx(xx).(xx).(xx).(xx).(xx).(xx).).(xx(x).).(xx(x).).(xx(x).).).(x(x).).(x(x).).(x(x).).(x(x).).(x(x).).(x(x).).(x(x).).(x.).(x(x).).(x(x.).(x.).(x.).(x.).(x.).(x(x.).(x.).(x.).(x(x)。

最佳回答

我不确定,如果我正确理解你的问题, 但我相信这可以是一个解决办法:

如果您这样写您的关联, 如果您想要 < code> ActiveRecord 或 < code > user. tatks , 则会自动创建加入 。

class User < ActiveRecord::Base
  has_many :clients
  has_many :jobs, :through => :clients
  has_many :tasks, :through => :jobs
end

更多信息见

问题回答

你们的协会似乎就从一边建立起来,只需要加上另一端,

class Task < ActiveRecord::Base
  belongs_to :job
end

class Job < ActiveRecord::Base
  belongs_to :client
  has_many :tasks
end

class Client < ActiveRecord::Base
  belongs_to :user
  has_many :jobs
  has_many :tasks, :through => :jobs
end

class User < ActiveRecord::Base
  has_many :clients
  has_many :jobs, :through => :clients
  has_many :tasks, :through => :jobs
end

活动记录将会为您处理组合。 的确, 在纯 db schema 中, 您不应该在不止一个地方拥有用户编号( 此处是客户表)。 但是有时它也会被添加到任务和工作表格中, 以提升性能, 因为这样 db 查询就不会那么大。 但是, 您必须更加努力地使您的数据保持一致 - 您必须保证工作与客户一样的用户编号。

这样您就可以定义快捷键关联,比如:

class Task < ActiveRecord::Base
  belongs_to :user
end

但在这种情况下,除非你注意到询问太慢,无法满足你的需求,否则我不会这样做。过早优化是邪恶的:-)





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...