从控制器轨道之外的模型获取数据

时间:2013-05-22 14:15:01

标签: ruby-on-rails

我需要获取所选订单的配置文件(打印订单)。

我的profiles表格有一个user_id列,而我的prints表格也有一个user_id列。

ProfilePrint模型belongs_to :user模型。

打印模型:

class Print < ActiveRecord::Base
  attr_accessible :comment, :document
  belongs_to :user
end

和,Profile模型:

class Profile < ActiveRecord::Base
  attr_accessible :address, :name, :phone
  belongs_to :user
  has_many :prints
end

如何根据用户从配置文件中获取打印数据?

1 个答案:

答案 0 :(得分:1)

假设您拥有print对象&amp; user has one profile然后

print.user.profile

Profile has_many :prints也是多余的,您可以直接使用

print.profile
相关问题