All threads / How do I display related or similar Ads by currency on the Ad show page?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I display related or similar Ads by currency on the Ad show page?

Charlie Kay asked in Rails
Hi Chris I need your help on this please.  I'm trying to display related Ads on the Ad show page by currency. But somehow  my script displays the same Ad in multiples of related Ads and not showing the actual related Ads. 

Ad
belongs_to :currency

Currency
has_many :ads

Ads Controller
def show
@related_ads = Ad.where.not(currency_id: @ad.currency_id)

show#view
<% @related_ads.each do |ad| %>
<%= @ad.name %>
<% end %>

Thanks
What is in the `@ad` variable you refer to in your show action?
Thanks a lot Jack! It should be 'ad' in the show page instead of '@ad' and ads_controller:
def show
@related_ads = Ad.where(currency_id: @ad.currency_id)
This works fine.
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,558+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.