2
class Contact < ActiveRecord::Base
has_many :contact_company_profiles, :dependent => :destroy
accepts_nested_attributes_for :contact_company_profiles, :allow_destroy => true
has_many :companies, :through => :contact_company_profiles
has_many :phones, :as => :phoneable, :dependent => :destroy
accepts_nested_attributes_for :phones
end
class ContactCompanyProfile < ActiveRecord::Base
belongs_to :contact
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :contact_company_profiles
has_many :contacts, :through => :contact_company_profiles
has_many :phones, :as => :phoneable, :dependent => :destroy
end
上述指定车型,我想通过接触控制器采用JSON格式响应代码工作正常,直到我被访问,直到公司的下方指定的命令。想在我的嵌套嵌套的JSON格式响应属性的Rails
@contacts = Contact.find(:id)
respond_to do |format|
format.html
format.js
format.json { render :json=>@contacts.to_json(:include=>[:companies, :phones) }
format.xml { render :xml => @contacts }
end
但是现在我想要在我的联系人控制器中嵌套公司的JSON嵌套。所以金德尔在这方面帮助我。 感谢
for activerecord我只会写':include => {:companies =>:phones}' – mpapis 2011-03-11 20:03:31