I have 3 models I am working with and trying to find the most frequent items between two models that are not related, but have a third table that is.
My 3 models are:
Visits (has_many Diagnoses)
Diagnosis (belongs_to Visit)
Prescriptions (belongs_to Visit)
Given a specific diagnosis, I want to know what drug is most commonly prescribed. Since Diagnosis and Prescriptions don't have a direct relationship I am lost. Below is my current code
This gets my top diagnoses
#top_diag = Diagnosiswork.group("diagnosis").order("count_diagnosis_id desc").limit(10).count("diagnosis_id")
This is in my view
<tbody>
<% #top_diag.each do |diagnosis, count| %>
<% top_visits = Diagnosiswork.where(diagnosis_id: diagnosis.id) %>
<% top_prescription = Prescription.where(visit_id: #top_visits) %>
<tr>
<td><%= count %>: <%= diagnosis.id %></td><td><strong><%= diagnosis.name %></strong></td>
<td>
<% top_prescription.each do |prescription| %>
<%= prescription.medicine.full_name %>
<% end %>
</td>
<td><span class="badge badge-success">Active</span></td>
<td>
</td>
</tr>
<% end %>
</tbody>
This gets me a list of drugs, but it is the same list for every diagnosis. My current thought is to get the Visits where these diagnosis are used the most, and then use a query similar to my top_diag that uses a where clause singling out certain visits.
Thanks so much in advance!
Related
I am implementing a HTML table in my Rails application. Currently, I have in my view:
<table>
<thead>
<tr>
<% #electives.each do |elective| %>
<th><%= elective.name %></th>
<% end %>
</tr>
</thead>
<tbody>
</tbody>
</table>
Elective and Course are models in my application. I have created a has_and_belongs_to_many association between them, thereby creating a courses_electives table.
At present, my webpage looks like:
What I want to do next is display all the courses related to each elective under the elective name.
I think I can achieve this using:
<% #electives.each do |elective| %>
<% elective.courses.each do |course| %>
<%= course.name %>
<% end %>
<% end %>
The problem I am facing with this approach is that it doesn't allow me to build my table vertically. I can create new <td> elements inside the above loops, but that will render data horizontally. That is, courses which belong to Software Engineering Elective A will end up being <td> entries in Software Engineering Elective B and so on.
What I want is:
How can I achieve this? Any help is very much appreciated. Thank you.
Sliim's suggestion is good. But what you're battling here is the limitations of html concepts that are over 30 years old. We now have grid and flexbox, which are much better ways to arrange elements on the page than tables. So how would this look in modern html? (well, I'll use haml b/c it's easier to see the structure, and b/c I'm lazy and haml rocks)
#electives
- #electives.each do |elective|
.elective
.name= elective.name
- elective.courses.each do |course|
.course= course.name
This just gives you a bunch of divs, but with a little css magic we can create the arrangement that you want:
#electives {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.elective {
display: flex;
flex-direction: column;
}
The grid-template-columns 'auto-fit' declaration adjusts to however many columns you may have, whilst setting minimum and maximum widths. It's described here.
Of course, the flexbox doesn't care that there are different numbers of courses in each elective.
You can use .transpose to flip the columns and the rows.
<% #electives.map(&:courses).transpose.each do |row| %>
<tr>
<% row.each do |course| %>
<td>
<%= course.name %>
</td>
<% end %>
</tr>
<% end %>
Do be careful with nil-s here, because I assume your electives might not have exactly the same amount of courses.
i want to achieve a nested loop without duplicates in a have and belongs to many relationship
i have a model 'campaign' and for each campaign i also have campaign data.
i want to display each campaign with its campaign data in a table. (nested)
#campaigns = current_user.campaigns
<% #campaigns.each do |item| %>
<% i = item.campaign_data %>
<% i.each do |cdata| %>
<%= cdata.date %>
<tr>
<td>
<%= item.name %>
</td>
<td>
<%= cdata.date %>
</td>
<td>
</td>
</tr>
<% end %>
<% end %>
my problem is that my campaigns get duplicated.
I want to achieve something like this:
Each campaign is listed in the table with its corresponding campaign_data directly below it, and if no campaign_data is left the next loop begins with the next campaign - is this possible?
best regard
You might be getting duplicated campaigns as you are using <%= item.name %> inside the <% i.each do |cdata| %> loop. So, if one campaign has 4 campaign_datas you will see the campaign name 4 times.
You should use naming conventions properly, if the campaign has many data campaign_data then you should specify so in association i.e. has_many :campaign_datas
Also, the Following code should be in the controller
#campaigns = current_user.campaigns.include(:campaign_datas)
Note:- I used include to avoid n + 1, please read here.
In view
<% for campaign in #campaigns %>
<% next if #campaigns.campaign_datas.blank? %>
<tr>
<td><%= item.name %></td>
</tr>
<% for campaign_data in #campaigns.campaign_datas %>
<tr>
<td><%= campaign_data.date %></td>
</tr>
<% end %>
<% end %>
Note:-
<% next if #campaigns.campaign_datas.blank? %> line is used to skip the campaign if it has no campaign data.
Rails newbie.. I'm getting an error on my join table index page.. but only sometimes. I'm confused since sometimes it works and sometimes it doesn't. Also when I go into pry and search for what is throwing the error, the item is there.
join table relationship:
post has many calendars/calendars has many posts
calendars_posts belongs_to calendars
calendar_posts belongs_to posts
calendar_posts controller:
def index
#calendars = current_user.calendars
end
index.html.erb:
<h1>Scheduled Posts</h1>
<% current_user.calendars.each do |calendar| %>
<h2> <%= calendar.name %> </h2>
<table>
<thead>
<tr>
<th>Title</th>
<th>Date</th>
<th>Time</th>
<th>Content</th>
<th>Link</th>
<th>Picture</th>
<th>Platforms</th>
<th>Finalized</th>
</tr>
</thead>
<tbody>
<% calendar.calendar_posts.each do |calendar_post| %>
<tr>
<td><%= calendar_post.post.title.titleize %> </td>
<td> <%= calendar_post.date%> </td>
<td><%= calendar_post.time %></td>
<td> <%= calendar_post.post.content %> </td>
<td> <%= calendar_post.post.link %> </td>
<td> <%= image_tag(calendar_post.post.picture_url, width: 200) if calendar_post.post.picture.present? %> </td>
<td>
<% calendar_post.post.platforms.each do |platform| %>
<%= platform.name.titleize %> <br>
<% end %>
</td>
<td> <%= human_boolean(calendar_post.post.finalized) %> </td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
I'm just confused because the error only happens sometimes... But once it starts happening then it happens every time I try to access the page after? Is there something I can do to ward off against nil?
I understand people are voting this down but I always do my research before posting and I finally figured out the issue. It was not like any of the other issues that showed up in related searches so it may be helpful for others to know... In my models I had Calendar and Posts with a join table CalendarPosts with user submittable attributes of time and date. The user can create a post and it floats separately from everything because I wanted someone to be able to jot ideas down as a "filler" and not be required to go further than that. This nil:nilclass error was coming from the fact that I had a dependency problem. My calendar_posts are dependent on posts. But I didn't have a dependency destroy in my post or calendar models. If a user deletes a post, the calendar post still existed but the post did not, which was the issue. Adding in the dependency destroy fixed my problem.
I'm building a small admin page for my app that will display data from 4 models in one table. The columns are: Clubs, Users, Posts, Comments.
A club has_many users, a user has_many posts and has_many comments.
So my questions is do I need to add pagination explicitly to each of my 4 models in my admin_controller? The way it is now, I get the page list on the top and bottom of my table, and I can go back and forward pages, but all of my results are shown on the first page (~9000 results).
In my admin_controller I have
#clubs = Club.all.paginate(page: params[:page], per_page: 50)
and in my view
<%= will_paginate #clubs %>
<table>
<% i = 0 %>
<tr class="new-admin-top-row">
<td><%= "Club Location" %></td>
<td>| <%= "Number of Signups "%> </td>
<td>| <%= "Number of Posts By Users"%> </td>
<td>| <%="Number of Comments By Users"%> </td>
</tr>
<%= #clubs.find_each do |club| %>
<tr class="new-admin-row">
<td class="new-admin-cell"><%= club.name %></td>
<td class="new-admin-cell f"><%= #users_array[i] %></td>
<td class="new-admin-cell s"><%= #posts_array[i] %></td>
<td class="new-admin-cell"><%= #comments_array[i] %></td>
<td class="new-admin-cell"><%= #elevates_array[i] %></td>
<% i+=1 %>
</tr>
<% end %>
</table>
<%= will_paginate #clubs %>
The find_each method works on ActiveRecord::Relation objects and fetches 1000 records in batches. So that is where you problem most likely is. Change it to each and it'll probably solve your issue.
You can read more about find_each here: http://apidock.com/rails/ActiveRecord/Batches/find_each
This is probably a very simple question, I apologise - I'm new to rails.
I've got 2 controllers - customers and orders. I've got it so that when a customer places an order, their id is passed in a hidden field, so that the customer_id field in the orders table has their id. So all orders have the id of the customer who placed it. What I'm trying to do is have the show view for the customers display all their orders - so all the orders with their id.
I've got the customer has_many orders and orders belong_to customers etc. How do I reference the customers/orders to extract the right info? And do I need to put extra info in the show action on the controller? I've tried everything I can think of! So far, I've been able to get a hash of all the info to appear in the show view, but I can't get individual bits of info to appear - e.g. order.price.
I basically want a table of the order details in the customers show view - order price, date placed etc. Any help would be much appreciated, thanks!
Edit: I now have this - but can't get the line_items bit to work. The relationships work like this: customer has many orders, orders have many line items, line items belong to products. I suspect the reason it's not working is because of the belongs_to.
<% #customer.orders.each do |order| %>
<% order.line_items.each do |line_item| %>
<tr>
<td><%= line_item.created_at %></td>
<% line_item.products.each do |product| %>
<td> <%= product.name %></td>
<% end %>
<td><%= order.email %></td>
</tr>
<% end %>
<% end %>
You shouldn't need to add anything to the controller show action.
In your customer show view you presumably have access to a #customer object. Because of your has_many, that will have a collection #customer.orders. So, in the view, you can do something like
<table>
<thead>
<td>Item</td>
<td>Quantity</td>
<td>Price</td>
<td>Date Placed<td>
</thead>
<% #customer.orders.each do |order| %>
<tr>
<td><%= order.item.name %></td>
<td><%= order.quantity %></td>
<td><%= order.price %></td></tr>
<td><%= order.date_placed %></td>
</tr>
<% end %>
</table>
Obviously I'm making up the possible order fields you'd want to display, but this should give you the idea.
In your show action:
def show
#customer = Customer.find(params[:id])
end
In your routes.rb:
resources :customers
In your view:
<table>
<% #customer.orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<td><%= And Other Your Order Fields %></td>
</tr>
<% end %>
</table>