2014-03-26 36 views
1
在应用

使用join_table表中的一个属性,我有如下代码:我怎么能接取一个模型

class UserProfile < ActiveRecord::Base 
    belongs_to :user 
    has_and_belongs_to_many :knowledge_areas, join_table: 'user_profiles_knowledge_areas', order: 'identifier' 


class KnowledgeArea < ActiveRecord::Base 
    attr_accessible :identifier, :label_key 
    attr_readonly :identifier, :label_key 

在我的数据库,我有一个表调用名称:user_profiles_knowledge_areas

我如何计算和访问此表中的记录?

我尝试了一些选项,比如:

<%= @teste = UserProfile.knowledge_areas.find(:all) %> 
<%= @a = UserProfile.user_profiles_knowledge_areas %> 

但任何作品,请,有人可以帮我吗?

对不起,我的英语,我学习又

回答

1

你的问题就是你直接调用UserProfile不初始化用数据填充对象&:

users = User.find(:all) 
for user in users do 
    puts user.user_profile.knowledge_areas 
end 
+0

魔兽,非常感谢,我明白了,谢谢=] –

+0

Np!希望它能帮助你! –