English 中文(简体)
2. 铁路在与同一模型有两条关系时命名公约
原标题:Rails naming conventions when having two relations to the same model

我的问题更多地涉及命名公约,而不是编程表一。

如果用户能够产生新的条款(因此是这些条款的所有人),并且你可以添加“编辑”条款,后者只能更新条款的内容。

class User
  include Mongoid::Document
  has_many :articles # as owner of the articles
  has_and_belongs_to_many :articles # as editor of the articles
end

class Article
  include Mongoid::Document
  belongs_to :user
  has_and_belongs_to_many :editors, :class_name =>  User 
end

我想知道的是,我如何把文章协会称作我的用户模式。 我指的是,一篇文章有作者和编辑,这在我看来是很强的命名公约,但用户有文章,是编辑。 你们将如何召集/宣布最后2个协会?

最佳回答

我将其称作<代码>:编辑_articles 和:著称_articles:所有_articles,或类似直截了当的名称。 Just dontabe to add the :class_name and :foreign_key or : through lifiers to them.

最新情况:

因为有——和——归属——到——关系,你需要一个连接表,而这个表是缺席的,是两个合并表格中的名字。 E.g. articles_users in You case. 在本表格中,您将预留两只女胎:user_idarticle_id。 这样,铁路就自动连接你的模型。

has_and_belongs_to_many :editors, :class_name =>  User , :foreign_id =>  user_id 

当然,如果你在表格中打电话到editor_id,则使用该编码。 相反,在用户方面也应努力。

问题回答

暂无回答




相关问题
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: ...

热门标签