Join Query on 2 tables(Rails 4) - ruby-on-rails

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

Related

Creating a resource that belongs_to another resource

Here are my relevant models:
class ListItem < ActiveRecord::Base
belongs_to :inventory_item
belongs_to :shopping_list
belongs_to :item
end
class ShoppingList < ActiveRecord::Base
has_many :list_items
belongs_to :user, :foreign_key => :user_id
end
class InventoryItem < ActiveRecord::Base
belongs_to :item, :foreign_key => :item_id
belongs_to :vendor
has_many :list_items
end
I want to have a button to create ListItems that belong to a user specified list that they own. The new ListItem also needs to be passed the respective :item_id and :inventory_item_id. Here's the relevant part of my current view:
<tr>
<% item.inventory_items.each do |product| %>
<td><%= button_to "#{product.price}",
{:controller => :list_items,
:action => 'create',
:id => #what goes here??,
:method => :create %></td>
<% end %>
</tr>
And my ListItems controller create method:
def create
ListItem.create
flash[:success] = "List Item Added."
redirect_to search_results_path(params[:search])
end
Clearly my create method isn't all that useful right now because it just creates a ListItem with no attributes other than :id. What's the best way to pass the appropriate parameters to my controller? Any help is much appreciated! Thanks in advance.
After doing a bunch of snooping around SO etc. I think the best way to accomplish this is to use a form with hidden fields, as below:
<%= form_tag("/list_items", method: "post") do %>
<%= hidden_field_tag(:item_id, item.id) %>
<%= hidden_field_tag(:inventory_item_id, product.id) %>
<%= hidden_field_tag(:shopping_list_id, ShoppingList.first.id) %>
<%= submit_tag("#{product.price}") %>
This is working well for me and is cleaner in this instance than using a button_to.

Rails: Create form for #score while in different model no direct associations

I want to create a multiple form for editing scores from a different model.
The main model is a Formrule model that consists of a habtm association with a Scoretype model
and has a habtm association with a Room model.
Both models are used to query a Scores model resulting in a #scores instance. It is for this instance I want to create a form, but the problem is that no field_for are being created. I know that the #scores is populated correctly, but the form does not show up.
This is the form as I have it now
<%= form_tag '/scores/update_scores' do %>
<table>
<tr>...</tr>
<% for score in #scores %>
<% fields_for :scores, score do |score| %>
<tr>
<td>
<%= score.hidden_field(:form_id) %>
<%= score.hidden_field(:team_id) %>
<%= score.hidden_field(:scoretype_id) %>
</td>
<td>
<%= score.number_field :scorevalue %>
</td>
</tr>
<% end %>
<% end %>
</table>
<%= submit_tag 'Update' %>
<% end %>
And these are the Models:
Formrule
class Formrule < ActiveRecord::Base
belongs_to :form
has_and_belongs_to_many :scoretypes
has_and_belongs_to_many :rooms
has_many :teams, :through => :rooms
end
Scoretype
class Scoretype < ActiveRecord::Base
has_many :scores
has_and_belongs_to_many :formrules
end
Room
class Room < ActiveRecord::Base
has_many :teams
has_and_belongs_to_many :formrules
end
Team
class Team < ActiveRecord::Base
has_many :scores
belongs_to :room
belongs_to :group
end
Score
class Score < ActiveRecord::Base
belongs_to :form
belongs_to :team
belongs_to :scoretype
validates_uniqueness_of :id, :scope => [:team, :scoretype]
end
And finally, the used controller (Formrule)
def show
#formrule = Formrule.find(params[:id])
#scoretypes = #formrule.scoretypes.all.collect
#rooms = #formrule.rooms.all.collect
#teams = Team.find(:all, :conditions => {:room_id => #rooms})
#scores = Score.order("team_id").all(:conditions => {:scoretype_id => #scoretypes, :team_id => #teams})
...
end
Why is the form not showing up? any suggestions?
Thank you all in advance!
Try using <%= fields_for ... %> instead of <% fields_for ...%>.

Rails 3 Data Modeling Help - Has Many, Belongs to, Nested Atrributes

I am working on a project involving three models (recipient, award, announcer) and need to have a nested attributes when issuing an award by an announcer to multiple recipients. For an example, award form need to have the ability to do 3 things:
Can add multiple-recipients (i.e. "add recipient", "remove recipient") - nested attributes
After creating a new award, the award will be posted into recipient's profile.
Enables future polling of #recipient.awards and #announcer.awards
Really struggle in terms of how to smartly solve this problem. The following data structure kind of made sense, however can not do "accepts_nested_attributes_for :recipients" in the award form. Can you help? Many thanks in advance.
class Recipient < ActiveRecord::Base
has_many :awards
has_many :announcers, :through => :awards
end
class Announcer < ActiveRecord::Base
has_many :awards
has_many :recipients, :through => :awards
end
class Award < ActiveRecord::Base
belongs_to :announcer
belongs_to :recipient
end
You're just about there. The main issue is that you're trying to create recipient objects in the form rather than just creating a relationship between the award and another object (user). You could do something like this:
class User < ActiveRecord::Base
has_many :recipients
has_many :awards, :through => :recipients
end
# this is your relationship between an award and a user
class Recipient < ActiveRecord::Base
belongs_to :user
belongs_to :award
end
class Award < ActiveRecord::Base
has_many :recipients
has_many :users, :through => :recipients
belongs_to :announcer
accepts_nested_attributes_for :recipients, :allow_destroy => true
end
class Announcer < ActiveRecord::Base
has_many :awards
has_many :recipients, :through => :awards
end
Then you would just do a nested form that would build the recipients_attributes array:
<%= form_for #award do |f| %>
<%= f.text_field :name %>
<div id="recipients">
<% #award.recipients.each do |recipient| %>
<%= render :partial => '/recipients/new', :locals => {:recipient => recipient, :f => f} %>
<% end %>
</div>
<%= link_to_function 'add recipient', "jQuery('#recipients').append(#{render(:partial => '/recipients/new').to_json})" %>
<% end %>
And, to keep it DRY just push the nested part into a partial:
# app/views/recipients/_new.html.erb
<% recipient ||= Recipient.new %>
<%= f.fields_for 'recipients_attributes[]', recipient do |rf| %>
<%= rf.select :user_id, User.all %>
<%= fr.check_box '_delete' %>
<%= fr.label '_delete', 'remove' %>
<% end %>
Obviously the User.all call isn't ideal so maybe make that an autocomplete.

Restricting a portion of code through an association

I have built a ruby on rails app that lets users track their workouts. User has_many workouts. In addition, a User can create a box (gym) if they are a gym owner. The purpose is to filter activity of users such that they can only see information related to their gym. Users can then specify if they are a member of that box through a Membership model. The Membership table collects #box.id and current_user.id in the membership.box_id and user.id columns respectively.
The user associates through the following form in the /views/boxes/show.html.erb view:
<% remote_form_for Membership.new do |f| %>
<%= f.hidden_field :box_id, :value => #box.id %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= submit_tag "I am a member of this box" , :class => '' %>
<% end %>
I then display, in the box show page all the users who are members of that box.
<% #box.users.each do |user| %>
<%= link_to (user.username), user %><br/>
<% end %>
I am trying to restrict the form to only users who are not already members of that box but I am not sure how to write the <% unless ... %> statement.
Here are the rest of the associations:
User
class User < ActiveRecord::Base
has_many :boxes
has_many :workouts, :dependent => :destroy
end
Workout
class Workout < ActiveRecord::Base
belongs_to :user
belongs_to :box
end
Box
class Box < ActiveRecord::Base
belongs_to :user
has_many :users, :through => :memberships
has_many :workouts, :through => :users
has_many :memberships
end
Membership
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :box
end
# Membership model
named_scope :for_box, lambda {|box| {:conditions => {:box_id => box.id}}}
# User model
has_many :memberships
def member_of_box?(box)
!memberships.for_box(box).blank?
end
# View
<% unless current_user.member_of_box?(box) %>
# Show the form
<% end %>
Disclaimer: The code is not tested. It might need minor alterations.

has_many through rails

Country Model
set_table_name "countries"
has_many :states, :primary_key => 'col1', :foreign_key => 'col1'
has_many :cities, :through => :states, :primary_key => 'col1', :foreign_key => 'col1'
State Model
set_table_name "strain_appendices"
belongs_to :country
has_many :states, :primary_key => 'col2', :foreign_key => 'col2'
City Model
set_table_name "ssu_accessions"
belongs_to :country
belongs_to :state
View
<% #countries.each do |country| %>
<%= country.high %>
<% country.states.each do |state| %>
<%= state.high %>
<% country.cities.each do |city| %>
<%= city.high %>
<%= country.high %> and <%= state.high %> give nice output. But, <%= city.high %> shows error as "uninitialized constant Country::City". Where may be the problem? Anybody help me?
Unless you define a method named high in City model or there's a high attribute, you'll get this error.
Maybe you're looking for city.country.high or city.state.high.
Edit
I'm sorry, I misread! As Shadwell said, you'd get another error in the situation described above.
In the State model, you are missing this:
has_many :cities

Resources