PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] how to specify has_many through vs has_many

 October 21, 2022     has-many, has-many-through, ruby-on-rails-4     No comments   

Issue

So I'm trying to figure out how to specify between calling all instances of an object based on the has_many and has_many :through associations. I have users, groups and members. the member model is a pairing of groups and users. the groups also have a user_id in that model. so this will make more sense after you see the code:

user model

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :groups
  has_many :groups, :through => :members
end

group model

class Group < ActiveRecord::Base
    has_many :users, :through => :members
    belongs_to :user
end

member model

class Member < ActiveRecord::Base
  belongs_to :groups
  belongs_to :users
end

Index View

<!--these are the groups I own (the group.user_id is my id) NEEDS EDITING-->
  <% @groups.each do |group| %>
      <%= link_to group.title, group_path(group) %>
      <%= truncate group.desc %>
  <% end %>

<!--I'm a member of these groups (connected through the member model) NEEDS EDITING'-->
  <% current_user.groups.order(:created_at).each do |group| %>
      <%= link_to group.title, group_path(group) %>
      <%= truncate group.desc %>
  <% end %>

I'm trying to figure out how to call the different types of associations users and groups in the Index View.

I want to get a list of all the groups that have me listed as admin (group.user_id) and then I want to get a separate list of all the groups that I'm a member of (through the member model).

I know I can query it with a find or where call. However, I was hoping there was a simple way like current_user.groups or something. anyway, thanks!


Solution

I would consider calling the 1:N relationship to groups another name. So you would end up with something like

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :owned_groups, class_name: 'Group'
  has_many :groups, :through => :members
end

So the index view would look something like:

<% current_user.owned_groups.each do |group| %>
    <%= link_to group.title, group_path(group) %>
    <%= truncate group.desc %>
<% end %>


<% current_user.groups.order(:created_at).each do |group| %>
    <%= link_to group.title, group_path(group) %>
    <%= truncate group.desc %>
<% end %>


Answered By - GSP
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing