Rails - Check if record has been updated - ruby-on-rails

I am trying to check whether a record has been updated/edited, and if it has, then it should display that is has been edited.
This is my if statement:
<% if comment.changed? %>
<p id="comment-name"><%= comment.user.name %>(edited)</p>
<% else %>
<p id="comment-name"><%= comment.user.name %></p>
<% end %>
Although it isn't working. When a comment is updated, no (edited) text is showing. What do I need to put in the if statement to check whether the comment record has been updated?

I don't think #changed? is doing what you think it's doing.
#changed? returns true if any attribute have unsaved changes, false
otherwise.
Reference
A record is edited if it's updated_at is different than created_at ( updated_at should be greater than created_at to be more precise).
<% if comment.updated_at != comment.created_at %>
<p id="comment-name"><%= comment.user.name %>(edited)</p>
<% else %>
<p id="comment-name"><%= comment.user.name %></p>
<% 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.

Rails how to make conditional inside iteration?

i'm trying to use a conditional inside a iteration but did not worked so, here the scenario:
in this case if the if the order or the product is present should just show the order and products with the feedback.
but even if is present show the feedback with odata and pdata.
someone know why?
<% #feedbacks.each do |feedback| %>
<% if order.present? && product.present? %>
<% order = feedback.order %>
<% product = order.product %>
<% else %>
<% odata = feedback.odata %>
<% pdata = odata.pdata %>
<% end %>
I guess this is what you are trying to do,
<% #feedbacks.each do |feedback| %>
<% if (order = feedback.order).present? && (product = feedback.product).present? %>
<%= order.title %>
<%= product.title %>
<% else %>
<%= (odata = feedback.odata).name %>
<%= odata.pdata.name %>
<% end %>
<% end %>
Note: title and name are assumed columns, replace it with your required/respected attribute.
Please go through this to understand the difference between various erb tags.
Comparison:
for the if condition you are trying to call order and product directly, which will throw error as they are related to feedback.
<% %> just executes the ruby code, you wanted to print the data so need to use <%= %>.
no need to save them in variable when you are not going to use it. I have saved them while checking the existence of the object in the condition and could use to display without querying the db.

Rails keeps ignoring an IF statement inside the view

I am writing an application where I want to list blog posts according to date they were created on the main index page. So lets say if I have 10 blog posts that were created today I would want to have smth like this come on the index page of the app..
Today:
POST 1
POST 2
POST 3
... so on
However, I have this...
Today:
POST 1
Today:
POST 2
Today:
POST 3
... so on
The following is my code..
index.html.erb
<% #posts.reverse.each do |post| %>
<% flag = true %>
<% if post.date.day < Time.now.day &&
post.date.day >= (Time.now.day - 1.day) %>
<!-- This IF statement gets ignored -->
<% if flag == true %>
<%= "Today" %>
<% flag = false %>
<br />
<% end %>
<tr>
<td><p>IMAGINE THIS SENTENCE IS ONE POST</p></td>
</tr>
<% end %>
<% end %>
The if statement that gets ignored inside the code acts as a flag, so that it should execute only once per group of posts with the same date. But, Rails seems to completely ignore it for some reason. If someone could help me correct this issue or nudge me in the right direction, your help would be greatly appreciated.
It's not that the if is being ignored, but that on each post the flag is being set to true again.
The first time you set the flag has to be outside of the iteration, like this:
<% flag = true %>
<% #posts.reverse.each do |post| %>
<% if post.date.day < Time.now.day &&
post.date.day >= (Time.now.day - 1.day) %>
<% if flag == true %>
<%= "Today" %>
<% flag = false %>
<br />
<% end %>
<tr>
<td><p>IMAGINE THIS SENTENCE IS ONE POST</p></td>
</tr>
<% end %>
<% end %>

Ruby on Rails how to get last query?

I'm having trouble trying to figure out when I reached the end of my query. So what I want to do is list all the records in my database that begin with the letter A which I got however I want to output a message if the query turns out blank. When I try I get a bunch of my custom messages even the query didn't turn out blank. Is there any way to tell if I've reached EOF in ruby on rails?
Sample
<div id = "content-A">
<p>A</p>
<% #animes.each do |anime| %>
<% if anime.aname.starts_with?('A') %>
<%= link_to anime.aname, {:action => 'list'} %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<%end %>
</div>
I believe you want sth like:
<% animes_group = #animes.group_by {|anime| anime.aname.to_s[0].upcase}
('A'..'Z').each do |letter| %>
<div id="content-<%= letter %>">
<p><%= letter %></p>
<% if animes = animes_group[letter] %>
<% animes.each do |anime| %>
<%= link_to anime.aname, {:action => 'list'} %>
<% end %>
<% else %>
<p>No anime listed in this Category :( </p>
<%end%>
<% end %>
You should consider moving some of the logic to the controller here, however what is to be moved depends on many factors like whether #animes are being used anywhere else etc.

(not) displaying optional fields in rails

(Disclaimer: I'm a bloody rookie)
My model contains some optional values. It seems that they may not exist at all (==nil) or that they may exist but are empty. In both cases, I don't want to show anything in my view. Currently, I do it like this:
<% if #score.lyricist and not #score.lyricist.empty? %>
<p>
<strong>Lyricist:</strong>
<%= #score.lyricist %>
</p>
<% end %>
This seems awkward and repetitive. Is there a better way?
blank? method will check both nil and empty values
<% unless #score.lyricist.blank? %>
<p>
<strong>Lyricist:</strong>
<%= #score.lyricist %>
</p>
<% end %>
Documentation here
present? method is the opposite of blank? method.
<% if #score.lyricist.present? %>
<p>
<strong>Lyricist:</strong>
<%= #score.lyricist %>
</p>
<% end %>

Resources