I have 3 tables: employees, projects and teams
Employee has_many :projects, :through => :teams
Project has_many :employees, :through => :teams
I added roles_id column to teams table.
Team belongs_to :role
Now I'm listing the projects, who's on the team and their role.
<% #project.employees.each do |employee| %>
<tr>
<td><%= employee.employee_full_name %></td>
<td><%= employee.team.role.rolecode %></td>
But, the role line is incorrect.
Any suggestions?
Your code has two problems:
Your associations are incomplete. See this explanation that
explains how to model a has_many :through;
An employee has_many teams, so you cannot say employee.team.
The solution for your first problem is that you have to define the following associations:
class Employee
has_many :teams
has_many :projects, :through => :teams
end
class Project
has_many :teams
has_many :employees, :through => :teams
end
class Team
belongs_to :role
belongs_to :employee
belongs_to :project
end
As for you second problem: an employee can belong to more than one team, so you cannot do employee.team. You can only do employee.teams. The solution would be to change your iteration like this:
<% #project.teams.each do |team| %>
<tr>
<td><%= team.employee.employee_full_name %></td>
<td><%= team.role.rolecode %></td>
roles_id => role_id ?) or belongs_to :role, :foreign_key => "roles_id"
Related
I have 3 Models
class User < ActiveRecord::Base
has_many :teams, :through => :team_memberships
has_many :team_memberships
end
class Teams < ActiveRecord::Base
has_many :users, :through => :team_memberships
has_many :team_memberships
has_many :clubs, :through => :club_memberships
has_many :club_memberships
end
class Clubs < ActiveRecord::Base
has_many :teams, :through => :club_memberships
has_many :club_memberships
end
I want to be able to get a unique list of clubs that the user is a member of. If I have the following:
#teams = User.last.teams
How can I get a list of clubs that these teams are members of. If there are any duplicates I would like to only show them once in the list.
Currently if I do:
<% #user.teams.each do |t| %>
<% t.clubs.each do |c| %>
<%= link_to c.name, c %>
<% end %>
<% end %>
I obviously get a complete list but I want to remove the duplicates. Can anyone offer a fix?
Looks like I can just set up a relationship like so:
class User < ActiveRecord::Base
has_many :clubs, -> { uniq }, :through => :teams
end
and then reference:
<% #user.clubs.each do |c| %>
<%= c.name %>
<% end %>
Please let me know if there is a better way!
I'm quite new to rails, so my question may seem noobish.
I have HABTM self association. A product can become a "COMBO" of products, having N products.
class Product < ActiveRecord::Base
has_many :combo_elements, class_name: "ComboElement", foreign_key: :combo_id
has_many :element_combos, class_name: "ComboElement", foreign_key: :element_id
has_many :combos, :through => :element_combos
has_many :elements, :through => :combo_elements
accepts_nested_attributes_for :elements
end
class ComboElement < ActiveRecord::Base
belongs_to :combo, :class_name => 'Product'
belongs_to :element, :class_name => 'Product'
end
With the above code, I can list all "elements" of a combo. Also I can list all "combos" a product is part of (the ComboElement table has a combo_id and an element_id)
Here is my form
<%= f.simple_fields_for :elements, #element do |b| %>
<%= b.input :element_id, :collection => Product.all.collect{ |t| [t.name, t.id ]}, selected: b.object.id %>
<%= b.link_to_remove "Remove this element" %>
<% end %>
I can successfully list all Products a combo has, but whenever I try to update, it simply doesn't work.
My Product controller has the following code
def product_params
params[:product].permit(:name, :price, elements_attributes: [:id, :element_id, :_destroy])
end
I appreciate any help.
Thanks in advance!
I having the following in my show.html.erb:
<% if #doctor.referrals_as_from.count > 0 %>
<% #doctor.referrals_as_from.each do |referral| %>
<%= referral.to_id %>
<% end %>
<% end %>
This works fine, giving me a list of matching id numbers from my referral model.
But, rather than getting a list of id numbers I would like to cross reference the "full_name" column from the Doctors model by using the identified id in referrals,basically an inner join. What is the most elegant way of doing this? Add a new method to the controller and to do a joins or includes, or is there a simpler way?
Models:
doctor.rb
class Doctor < ActiveRecord::Base
self.primary_key = "npi"
has_many :referrals_as_from, :class_name => 'Referral', :foreign_key => 'from_id'
has_many :referrals_as_to, :class_name => 'Referral', :foreign_key => 'to_id'
end
referral.rb
class Referral < ActiveRecord::Base
belongs_to :doctor
end
Use this.
<% if #doctor.referrals_as_from.count > 0 %>
<% #doctor.referrals_as_from.each do |referral| %>
<%= referral.doctor.full_name %>
<% end %>
<% end %>
The Referral model actually has two doctors associated with it, as defined by to_id and from_id. So you might need to do the following:
referral.rb
class Referral < ActiveRecord::Base
belongs_to :to_doctor, :class_name => "Doctor", :primary_key => "to_id", :foreign_key => "npi"
belongs_to :from_doctor, :class_name => "Doctor", :primary_key => "from_id", :foreign_key => "npi
end
Then the code becomes
referral.to_doctor.full_name
I am providing my active records below. In view/users/show I want to display any project the user is working on, through blueprints. When a user adds multiple blueprints to a project, the project is showing up multiple times. I tried some validate_uniqueness options to no avail.
class Blueprint < ActiveRecord::Base
attr_accessible :id, :name, :project_id, :user_id, :loc
belongs_to :user
belongs_to :project
has_many :comments
end
class Project < ActiveRecord::Base
attr_accessible :id, :name
has_many :blueprints
has_many :users, :through => :blueprints
has_many :comments
end
class User < ActiveRecord::Base
attr_accessible :id, :name
has_many :blueprints
has_many :projects, :through => :blueprints
end
Here is the view code that is displaying multiple values of the same project.
<% #user.blueprints.each do |blueprint| %>
<tr>
<td><%= link_to blueprint.project.name, project_path(blueprint.project) %></td>
</tr>
<% end %>
Thanks!
Try setting uniq option to true in user's projects relation like this
class User < ActiveRecord::Base
has_many :projects, :through => :blueprints, :uniq => true
end
Since you already have the projects association in the User, why don't you loop through the user's projects instead of the blueprints.
<% #user.projects.each do |project| %>
<tr>
<td><%= link_to project.name, project_path(project) %></td>
</tr>
<% end %>
I have a recipe, ingredient, Ingredient_Recipe models
recipe has
has_many :ingredient_recipes
has_many :Ingredients, :through => :RecipeIngredient
ingredient has
has_many :ingredient_recipes
has_many :Recipes, :through => :RecipeIngredient
Ingredient_Recipe has
belongs_to :recipes
belongs_to :ingredients
in my ui this doenst work anymore
<% #recipe.ingredients.each do |ingredient| %>
EDIT
ActionView::Template::Error (uninitialized constant Recipe::Ingredients):
97: </td>
98: <tr>
99: <td >
100: <% #recipe.ingredients.each do |ingredient| %>
101: ingredient.name
102: <% end %>
103: </td >
Change:
has_many :Ingredients, :through => :RecipeIngredient
to
has_many :ingredients, :through => :ingredient_recipes
Don't capitalise :ingredients, and :through needs to reference the association you're going through rather than the model.
For :recipes:
has_many :recipes, :through => :ingredient_recipes