开发者

Has_and_belongs_to_many in Rails 3

开发者 https://www.devze.com 2023-03-25 04:52 出处:网络
I\'ve tried to look through stackoverflow to remedy my situation but unfortunately I\'ve just made myself thoroughly confused.

I've tried to look through stackoverflow to remedy my situation but unfortunately I've just made myself thoroughly confused.

I have two has_many to has_many relationships: Users has_and_belongs_to_many Roles, and Roles has_and_belongs_to_many Pods. I've already set up the correct tables and everything (i.e. the role_users and pod_roles tables): I know it's at least partially working because I'm able to do @user.roles and get all of that user's roles.

However, I want to take it one step further: I want to be able to do something akin to @user.roles.pods to get all of the pods pertaining to that user. Af开发者_如何学Cter @user.roles.pods didn't work: here's what I tried to do:

@current_user_roles = current_user.roles
@pods = Array.new
@current_user_roles.each do |role|
  @pods.push(role.pods)
end
@pods.each do |pod|
  puts(pod.name+"-----------------")
end

Didn't work. This is just to try to get something super simple -- to try to get all of the pods' names to appear to I can tell if it's working.


Here's something you could try:

@pods = current_user.roles.collect { |role| role.pods }.flatten
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号