2013-07-06 62 views
2

我试图用wice_grid宝石的has_many通过协会wice_grid

user has_many :roles, :through => :assignments 

role has_many :users, :through => :assignments 

在users_controller.rb

@users_grid = initialize_grid(User, 
            :include => [:assignments, :roles]) 

我应该怎么在视图中写才能得到用户的网格表中的作用使用户的角色出现,我不能从文档中获得它,所以需要帮助,请如何将它写在视图中?

回答

2

只需尝试以下操作。让name是用户表中的列,role_name是角色表中的列。

<%= grid(@users_grid) do |g| 
    # Here I have defined the column name. Column names are defined with parameter ':name' and the ':attribute' defines which column to map in the users table for this column. 
    g.column :name => 'User Name', :attribute => 'name' do |user| # primary table 
    link_to(user.name, user_path(user)) 
    end 

    # Regarding join tables or associations, we need to specify the model name as here, I have defined model: 'Role'. 
    g.column name: 'Having Roles', attribute: 'role_name', model: 'Role' do |user| 
    # here we are using the associations showing the data for the joints table. 
    user.roles.collect{|role| role.role_name}.to_sentence 
    end 

    g.column do |user| 
    link_to('Edit', edit_user_path(user)) 
    end 
end%> 

可以检查样本示例代码https://github.com/leikind/wice_grid_testbed

+1

真的谢谢:)但你能解释这对我好吗? –

+0

@莫斯塔法侯赛因,我已经添加了评论给我的答案解释。 –

+0

你能帮我在这个http://stackoverflow.com/questions/17495228/private-pub-issue-in-rails-application,我不能找到一种方法来解决这个 –