combining 2 tables, looping through the results and displaying items from each - ruby-on-rails

So I'm trying to combine two tables and show the results in order of the start_date.
I've tried a few things but because its technically a nested loop its giving me double results for each item.
The code i currently have is as follows
<% #subcategory = Subcategory.all %>
<% #product = Product.all %>
<% (#product + #subcategory).each do |product, subcategory|%>
<% if product.display_on_home_page and !product.is_highlight_product and !(product == '..') or subcategory.
display_on_home_page and !subcategory.is_highlight_product and !(subcategory == '..')%>
<div class="column_entry">
<%= link_to image_tag(subcategory.image_attachment.url(:normal_page_size)), subcategories_content_url(subcategory.id), :controller=>'subcategories' %>
</div>
<% end %>
<% if product.price_from %>
<div class="column_entry">
<div class="product_special">
<span class="a">From Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% else %>
<div class="column_entry">
<div class="product_special">
<span class="a">Only</span>
<span class="b"><%= number_to_currency(product.price,:unit=>'€') %></span>
</div>
<%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id), :controller=>'products' %>
</div>
<% end %>
<% end %>
I know this is quite a long an complex statement, its supposed to loop through all of the subcategories and all of the products and display the images, there are also two different ways of displaying the price based on a boolean that says whether the price is a specific amount or it starts from a given price.
at the moment its reading through the loop but its giving me the error
undefined method `is_highlight_product' for nil:NilClass
since this is the first column in the table that is referenced and its breaking here I think that there must be some conflict in its ability to see the information stored in the table.
I'm still quite new to ruby on rails so any help or even just a nudge in the right direction would be very much appreciated.
If you would like more information just ask in the comments and I'll put it up as fast as I can.

The problem here is, when you do something like this:
(#product + #subcategory).each do |product, subcategory|
The local variable product will iterate firstly through products, then through subcategories, and the local variable subcategory will always be nil.
What you can do, a dirty way - check
if product.is_a?(Product)
# do your things
elsif product.is_a?(Subcategory)
# do other things
end

Related

Rails HTML Multi Layered IF checking

I am working on a layout that requires a couple of checks to the database, but I cannot figure out the logic on the IF statement. I have a People table where each record holds a Position field. There are times that specific positions will be held by two people. In which cases they want to be Co-Position. So I current am listing said Position on my page with:
<li>
<% #people.each do |person| %>
<% if person.position == 'Office1' %>
<div class="image">
<%= link_to(person_path) do %>
<%= image_tag("profiles/#{person.uname}S.jpg") %>
<% end %>
</div>
<div class="body2">
<h4>Office</h4>
<%= link_to(person_path) do %>
<h5><%= person.fname %> <%= person.lname %></h5>
<% end %>
</div>
<% end %>
<% end %>
</li>
Now within that I need to run a check at <h4>Office</h4> to see if another record holds Office2, but if I place an IF statement around the H4 tags will it be checking the already fetched record or will it check the entirety of the Person database? Do I need to do this check first and then do an IF/Then or can this be done in a more streamlined way?
Basically I need to check if Office2 is present somewhere in the Position field, and if it is I need the H4 to read Co-Office, else I need it to read Office.
If your condition is if person.position.include?('Office2') then no, it won't touch the database again. The person object is loaded with all its attributes.

Current user information is displaying on all profiles Ruby on Rails

So I am making a Social media website where user join events and compete against each other.
Each time a user join an event a status appears on his profile. The issue is that when I look at other user profiles, the statuses just match who ever the current user is.
If someone could help me write some code that shows the different statuses for each of the users that would be great. I know the question may sound a little vague.
I just need to know a way to see only post based on which profile I am viewing not the current user. Thanks!
<div class="center">
<div class="col-xs-6">
<div class="panel panel-default">
<div class="panel-body">
<% if !#user_events.present? %>
<h4>Hi <%= User.find_by_username(params[:id]).username %>, thanks for joining!</h4>
<h4>Join events, complete them, and compete</h4>
<% end %>
<% #user_events.each do |ue|%>
<% if ue.event.present? %>
<h4 class="blue">
<%= User.find_by_username(params[:id]).username %>
</h4>
<% userevent = User.find_by(params[:event]) %>
<p>I'm going to <%= link_to ue.event.title %>.</p>
<% if ue.hours.present? %>
<h5> Completed Hours: <%= ue.hours %> </h5>
<% end %>
<% if ue.hours.present? %>
<img src="#" style="width: 10%;" alt="">
<% end %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
First of all, it's very bad to have queries to the database inside of your view. Please, move expression inside <%= User.find_by_username(params[:id]).username %> to your controller and then use instance variable to show username.
It would be great if you provide your controller code too. I guess you should pick user events depending on the user you found by params[:id].
Without the controller, it is a bit hard to help. But I would suggest trying something on the user controller like
def get_status_by_user
user_id = params[:user_id]
user = User.find_by(id: user_id)
user.event
end
where the params[user_id] is the id of hte user you want to get the event.
Also what I can see is <% userevent = User.find_by(params[:event]) %> you do not specify a user so the method will return the last one
Finds the first record matching the specified conditions.
Find_by docuemntaion
So if you want to keep your code mostly as it is you need to first find the user you want to get the event and then get the status for that specific user
Also giving a little information on the DB schema could help

Combining two associations in my "each do" loop method

I'm a little new to back-end programming...I'm currently running the following code in my rails 4 app to show a basic list of all the admins on a project (if there are any)...
<% if project.projectadmins.any? %>
<div class="row-fluid">
<% project.projectadmins.each do |user| %>
<div class="collaborator">
<%= link_to user do %>
<%= image_tag user.image_url(:thumb).to_s, :class => "profile-pic-thumb" %>
<% end %>
</div>
<% end %>
</div>
<% end %>
However, I also have projectcollaborators for each project, so I'd like to know what the most effective way would be to combine those and provide a list of both projectadmins AND projectcollaborators (with project admins being listed first if there are any...other than that ordering is not important).
I assume the if statement at the beginning would change to...
<% if project.projectadmins.any? || project.projectcollaborators.any? %>
but I'm not 100% sure and am lost on the rest...any help is much appreciated.
You could create a scope, for example project_admins_and_collaborators, which gets all the needed records and then use it in your loop.
You can also do this in following way
project_admins_and_collaborators = project.projectadmins
project_admins_and_collaborators << project.projectcollaborators
project_admins_and_collaborators.flatten.uniq do |user|
#your code
end

Listing objects under the date they were created

Sorry for the amateur question but I am still quite new to rails. I currently have an app that creates jobs and I would like to display the jobs beneath the date they were created in the same way they do on Dribble
At the moment to display the jobs I have te following:
<% #jobs.each do |job| %>
<div class="job-wrapper"><%= link_to user_job_path(job.user_id ,job) do %>
<div class="job">
<div class="job-desc"><strong><%= job.company %></strong> are looking for a <strong><%= job.job_title %></strong>
<div class="job-sal"><%= job.job_salary %></div>
</div>
</div>
</div>
<% end %>
<% end %>
I am sure I need to create a loop of some kind to make this work but am unsure how to incorporate it so that the date only displays once at the top of the jobs and then all jobs created during that date are shown?
Any help would be much appreciated! Thanks
Try the pattern below --
<% #job.group_by{|x| x.created_at.to_date }.each do |date,jobs_on_that_date| %>
<%= date %>
<% jobs_on_that_date.each do |job| %>
# render the job
<% end %>
<% end %>
Basically you need to group your jobs by the date (or whatever you want to group on) then get a hash keyed on the stuff you grouped on. Then render the key (date) followed by the list of objects relating to that key.

Ruby on Rails: get collection of attributes for a model

I have a model with a lot of attributes, and have build a series of pages to collect all the relevant data. In the last page, I want to show the user all the collected data.
I could create this page by manually typing all the labels and values for each attribute, but I expect that this kind of tedious and repetitive task has already been solved by someone so that in 3-4 lines of code.
At this stage I am only prototyping so this doesn't need to look good.
Anyone has any suggestions as to how to quickly print on the screen all attributes of a model?
I was thinking something like this:
If #my_data_model is the instance variable of which I want to print the attributes, then:
<%= show_attributes #my_data_model %>
would output the attribute values with their labels.
Thanks in anticipation.
I am doing that for one of my projects like this:
First I define an array of the columns I don't want like the timestamp columns:
<% #rejects = ["id", "created_at", "updated_at" %>
Then from the object I remove those columns;
<% #columns = Patient.column_names.reject { |c| #rejects.include?(c) } %>
Then I iterate through the column_names and print out the entered information:
<h2>Is the following information correct?</h2>
<div class="checks">
<h3>Patient details</h3>
<% #columns.each_with_index do |c, i| %>
<p id="p<%= i %>" class="check">
<span class="title"><%= c %>:</span>
<span class="value"><%= #patient[i] %></span>
<span class="valid">
<img src="../../images/icons/tick.png" alt="green tick">
</span>
</p>
<% end %>
</div>
Hope this helps!
I have been using this as a generic show view for inheritated_resources gem.
%h2= resource_class.model_name.human
%table
- resource_class.column_names.each do |column_name|
%tr{ :class => (cycle "odd", "even") }
%td= resource_class.human_attribute_name(column_name)
- if resource[column_name].respond_to?(:strftime)
%td= l resource.send(column_name)
- else
%td= resource.send(column_name)
There resource_class returns the current model class and resource the current instance of it.
Thank you all,
I build a solution based on your recommendations like this:
<% #rejects = ["_id", "created_at", "updated_at"] %>
<% #columns = Agency.column_names - #rejects %>
<% #columns.each_with_index do |c, i| %>
<p id="p<%= i %>" class="check">
<span class="title"><%= c %>:</span>
<span class="value"><%= #agency.send(c) %></span>
</p>
<% end %>
Using <%= #patient[i] %> didn't work for me, probably because I am using Mongomapper as my ORM.

Resources