# models
class A < ActiveRecord::Base
has_many :b
class B < ActiveRecord::Base
belongs_to :a
# controllers
class aController < ApplicationController
def a_with_b
@a_with_b = A.find(1, :include => [:b])
puts @a_with_b # => #<A id:1> // `b` not mapped to `@a_with_b`
puts @a_with_b.b # => [#<B id:1>, #<B id:2>] // still there's `b`
end
end
问题:
如何b
映射到@a_with_b
?
预期的:
puts @a_with_b # => #<A id:1 b:[#<B id:1>, #<B id:2>] >
上面写的所有实际原因是能够获得具有适当结构的序列化对象:例如
{something: true, nothing: false, a: @a_with_b}.to_xml # =>
<xml>
<something>true</something>
<nothing>false</nothing>
<a>
<id>1</id>
<bs>
<b> <id>1</id> </b>
<b> <id>2</id> </b>
</bs>
</a>
<xml>
导轨 v.2.3