2013-05-22 48 views
0

我需要獲取選定訂單的配置文件(打印訂單)。從控制器滑軌外的模型獲取數據

我的profiles表有一個user_id列和我的prints表也有一個user_id列。

ProfilePrint模型belongs_to :user模型。

打印模式:

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

和剖面模型:

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

如何根據用戶從打印配置文件獲得數據?

+0

什麼是您的用戶模型是什麼樣子?用戶是否有多個配置文件或只有一個? – Mischa

+0

由於您在配置文件中擁有'has_many:prints',您不應該在Print上擁有'belongs_to:profile'嗎? – depa

+0

打印和配置文件都屬於用戶。如果配置文件是打印 –

回答

1

假設你有print對象& user has one profile然後

print.user.profile 

而且Profilehas_many :prints這是多餘的,你可以直接使用

print.profile 
+0

對於假設的父母,這是可能是錯誤的,我想:地址欄, 我想說print.user.profile.address 同樣適用於:電話:命名 肯定的,has_may :打印是多餘的,我刪除了。 謝謝! :) – muhammadn

+0

是的,一旦你得到配置文件,你可以調用'profile.address' – Salil