- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个模型,Couple
,它有两列,first_person_id
和 second_person_id
和另一个模型,Person
,其主键是 person_id
并且具有列 name
这是我想要的用法:
#including 'Person' model for eager loading, this is crucial for me
c = Couple.find(:all, :include => :persons)[0]
puts "#{c.first_person.name} and #{c.second_person.name}"
那我该怎么做呢?
最佳答案
在 Couple
中声明的关系应该是这样的:
class Couple
named_scope :with_people, { :include => [:first_person, :second_person] }
belongs_to :first_person, :class_name => 'Person'
belongs_to :second_person, :class_name => 'Person'
end
#usage:
Couple.with_people.first
# => <Couple ... @first_person: <Person ...>, @second_person: <Person ...>>
那些在Person
取决于是否 Person
可以是多个的一部分Couple
.如果 Person
只能属于一个Couple
不能成为“第一个”Person
在一个和Second
另一方面,您可能想要:
class Person
has_one :couple_as_first_person, :foreign_key => 'first_person_id', :class_name => 'Couple'
has_one :couple_as_second_person, :foreign_key => 'second_person_id', :class_name => 'Couple'
def couple
couple_as_first_person || couple_as_second_person
end
end
如果 Person
可以属于几个 Couple
s,并且没有办法判断它们是任何给定 Couple
中的“第一个”还是“第二个” ,你可能想要:
class Person
has_many :couples_as_first_person, :foreign_key => 'first_person_id', :class_name => 'Couple'
has_many :couples_as_second_person, :foreign_key => 'second_person_id', :class_name => 'Couple'
def couples
couples_as_first_person + couples_as_second_person
end
end
关于ruby-on-rails - ActiveRecord has_many 其中表 A 中的两列是表 B 中的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2125440/
我有一张用户表 users +------+ - user_id ... ... - updated_by 据我所知,updated_by 列应该在 user_id 列上引用此表。我不确定,这
我是 SQL 新手,我有 2 个包含这些列的表: table structures__|id|name|nation_id|image| table nations______|id|nation|
我正在使用 codeigniters sql 选择来选择不在一组 id 中的用户。 $this->db->select('fbuid')->where_in('fbuid', $friends); $
找不到具体的答案所以我想问一下。简而言之,我有一个表,它根据存储在我的数据库中的数据从 API 检索信息,我想要做的就是从该表中获取某些列的总数,而不是全部列,以便我可以在其他地方使用它们网站。例如,
这是 phpMyAdmin 为我生成的 SQL 语句: SELECT * FROM `table_name` WHERE 1 可以看到 table_name 被 ` 字符包围。 为什么? 最佳答案 用
我有两个这样的表 我想在这里从 Table1 插入到 Table2。这就是我想要的。 取 MOU = 10。它在同一行中有 num1 和 hour1。我想将它插入到与 num1 同一行与 hour1
我的任务是使用 C++ 编写一个程序,以使用 Gamma 分布计算概率。如果我已经找到函数值,如何将其更改为 Gamma 分布表中的值?我不知道公式。 例如Fg(8;8),在表中为0.5470。而表中
我在查看 HTML 电子邮件时遇到问题,需要格式化 css 以与 HTML 脚本内联。 我有多个表使用的以下 td css。 td.gridtopleft { border-left: solid
我正在使用来自 pip 的最新 sqlalchemy 和最新的 pymssql 连接 mssql 服务器 8.00.2039(2005 年?)困难在于表和列名称是俄语。是否可以用 sqlalchemy
我有一个有趣的问题 - 我需要 JOIN 语句的完全相反。 我有一个名为 invoices 的表和另一个名为 payments 的表。我想按顺序检索没有任何付款的发票,但之后是有付款的行。 发票表有这
我有两个表: 主题:[id, ...] 类别:[主题.id, ...] 我想从表 #1 中选择所有主题,但不包含 #2(类别)中的条目。 任何提示表示赞赏(: 最好的问候 最佳答案 Sachin 已经
MYSQL: 我正在使用表 information_schema.tables 中的 AUTO_INCRMENT 列来获取下一个 id。 如下: SELECT AUTO_INCREMENT FROM
我是一名优秀的程序员,十分优秀!