Rails HTML Multi Layered IF checking - ruby-on-rails

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.

Related

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

How do I find all the relatives of a node in ancestry?

I am using the ancestry gem, and what I want to do is always detected all the related posts to the current post.
So my Post has_ancestry.
In a related posts section of my view, I have this:
<% if #post.has_children? %>
<p>
<ul>
<% #post.children.each do |child| %>
<li><%= link_to child.title, post_path(child)%></li>
<% end %>
</ul>
</p>
<% else %>
<p>
There are no related posts.
</p>
<% end %>
</div>
While that approach is fine, it just checks one use case (i.e. if this post has a child). What I want to happen though is when the person clicks through to the child, on that show page it should show this post as a related post too.
In effect it would be a parent, but I don't want to have to do if post.has_parents?, then if post.has_children?, if post.has_siblings?, etc.
Ideally, I would just like for any relatives to be detected, and if there are relatives I want to loop through them all and display them all in a homogenous way.
How do I do that?

combining 2 tables, looping through the results and displaying items from each

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

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.

Rails: Generate Unique Random Number for populating a CSS class String How To?

So let's say we have a rails view with this code:
<h1>Users who have the <%= #title.name %> Title</h1>
<ul>
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-X"></div></li>
<% end %>
</ul>
And what we want to do is to automatically print this line:
<div class="rw-ui-container rw-urid-X"></div>
Right aside each username the loop throws at us BUT we want X to be a different unique random number each time...
How can this be accomplished?
May be this solution can help you:
1. You can use user_id as first part of the random X to ensure that
another user doesn't have it
2. Second part is time in UTC format to set X different each time
3. Third part is just a standart random func. You haven't use it if you want.
<h1>Users who have the <%= #title.name %> Title</h1>
<ul>
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-<%= "#{user_id}#{Time.now.utc.to_i}#{rand(100000)}".to_i %>"></div></li>
<% end %>
</ul>
css_arr = ["rw-ui-container rw-urid-1","rw-ui-container rw-urid-2","rw-ui-container rw-urid-3"]
<div class="<%= css_arr.sample %>"></div>
Well You could easily add a new field to user table call
uuid or give it any name that you like and assign a unique id using the gem called uuid
This help ensure that you have uniqueness in X section and also on refresh the value wont change as you mention in one of your comment
so here how your X code would look like
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-<%=user.uuid %>"></div></li>
<% end %>

Resources