How do I find all the relatives of a node in ancestry? - ruby-on-rails

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?

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

rails if current id do

I'm developing an online course site using Ruby on Rails and bootstrap. I have a model for Courses and a model for Lessons. In the lesson controller show view I am not only showing the lesson page with video, discussion and notes but also a list of all the lessons belonging to the course. This is done by:
<%= #lessons.each do |lesson| %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
The user can, from the individual lesson page, pick the next lesson to watch.
Question:
The URL generated is: localhost:3000/courses/1/lessons/2 (I'll change this with Friendly Id gem later). I would like to show in the list of all lessons on the individual lesson page which lesson the user is watching right now. So basically maybe have something say "You are currently watching : Lesson with id 2" and have it in a different background color with some custom html. How can I have different HTML and CSS for the currently watching lesson in the all lessons list?
Thanks a lot in advance!
/Jacob
A simple way would be to add a conditional, in the case you have the current lesson available in a local variable or method current_lesson. Then it can look like this:
<%= #lessons.each do |lesson| %>
<% if lesson.id == current_lesson.id %>
... other html ...
<% else %>
<%= link_to [lesson.course, lesson] do %>
<%= lesson.title %>
<% end %>
<% end %>
<% end %>

How to access specific object fields?

I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>

Stuck trying to list associated model

I think I am deeply misunderstanding how to write instances.
Miniatures have_many Manufacturers through the Productions table.
On the miniatures show page I am trying to list all the manufacturers for the current miniature and have them link the Manufacturer show page. Like so:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturer.name, manufacturer_path %>
<% end %>
Needless to say it does not work. It gives "undefined method `manufacturer'".
I have tried A LOT of different combinations to no avail. The following version puts all the manufacturers, rolled into one link, once for each manufacturer a miniature has, and links to /manufacturers. A big mess.
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturers.map(&:name).join(', '), manufacturer_path %>
<% end %>
I have been working on other things and hoping I would get the hang of this but I'm pretty sure it's something pretty fundamental about how I set up the instance.
If it's more likely something I need to add to the controller then I can add my controller code here. Any help much appreciated.
Does this work:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to manufacturer.name, manufacturer_path(manufacturer) %>
<% end %>

Resources