Using two different models, RecordLabel
and Artist
, I want to link to their pages if the record is found using their usernames. I have no problems finding if the record exists, but I can't figure out how to find the ID of that record. What I have:
<% if RecordLabel.exists?(:username => "#{@artist.artist_profile.record_label_name}") %>
<%= link_to @artist.artist_profile.record_label_name, record_label_path(RecordLabel.find(### NEED RECORD LABEL ID ###) %>
<% else %>
<%= @artist.artist_profile.record_label_name %>
<% end %>
答案 0 :(得分:1)
You can get the record very easily this way (if it exists):
RecordLabel.where(:username => "#{@artist.artist_profile.record_label_name}").first
So, your code becomes:
<% if RecordLabel.exists?(:username => "#{@artist.artist_profile.record_label_name}") %>
<%= link_to @artist.artist_profile.record_label_name,
record_label_path(RecordLabel.where(:username => "#{@artist.artist_profile.record_label_name}").first) %>
<% else %>
<%= @artist.artist_profile.record_label_name %>
<% end %>
This should work and solve your problem.