I'am trying to display school name which is related to members, but I got the School:0x00559eba30b8b8, instead the name.
<% #members.each do |member| %>
<%= member.name %>
<%= member.email %>
<%= member.school %>
<% end %>
result : John john#john.com #School:0x00559eba30b8b8
update:
with school_id I get the id of the school, but still don't know how to get the name.
How can I get the name of the school?
Many thanks
Since school is not mandatory attribute, it can’t be just queried for the name, one should use try there:
<%= member.school.try :name %>
Assuming you have school_id in members table you need to print school.name instead of school
<% #members.each do |member| %>
<%= member.name %>
<%= member.email %>
<%= member.school.try(:name) %>
<% end %>
NOTE: member.school will return you an instance of School and you want to print just name.
Use
<%= member.school.try(:name) %>
Note:- Assuming your school has attribute 'name'
Also in your controller where you finding #members use 'includes', something like
#members = member.where().includes(:school)
Related
I have a #friend model that has_and_belongs_to_many #interests and vice versa. Each interest has a name:string. How do I show all the interests by their name next to each friend?
I tried
friend.interests.count
which shows the correct number, but for
friend.interests.first
the result is
#<Interest:0x00007f959e103250>
How do I display the name of this interest from the database in a view?
<%= friend.interests.count %>
<%= friend.interests.first %>
You can get the name of the interest, just by accessing the friend.interests.first.name. And for listing all the interests you can iterate and show the name of them.
<% friend.interests.each do |interest| %>
<%= interest.name %>
<% end %>
Just put the attribute after the object:
<%= friend.interests.first.name %>
I have to models:
Father has_many Children
f_name
Child belongs_to Father
c_name
father_id(fk)
In children's index page I want to show c_name and fathers' name
<% #children.each do |child|%>
<%= child.name %>
<% if Father.find(child.father_id) %>
<%= Father.find(child.father_id).f_name %>
<% end %>
<% end %>
I do not think the code is elegant. Maybe I should put them into helper or model, but I do not know how to do that.
Anybody help will be appreciated.
I'm not sure how your controller looks like, but it can be like this.
#children = Child.includes(:father)
in view:
<% #children.each do |child|%>
<%= child.name %>
<%= child.father.try(:name) %>
<% end %>
try does same as <%= child.father.name if child.father %>
If you have your relationships correctly setup in your models then rails will give you some nice helper methods. In this case to find a child's father you can do: child.father. Then of course child.father.name to get the name of the father.
If you're worried that a child does not have a father then you could do something like:
<%= child.father.name if child.father %>
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 %>
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.
I came from a JS background so Rails is weird to me. I currently have a show.html.erb for the contest model:
<h1>Contest name: <%= #contest.name %></h1>
<h2>Contest criteria: <%= #contest.criteria %></h2>
<h3>Photos: </h3>
<%= link_to "Enter Contest", "#" %>
<% #contest.photos.each do |photo| %>
<%= image_tag("#{photo}") %>
<% end %>
With the link_to I'm trying to render all photos that belongs to the current_user and pick one of them to assign it to the current contest. The params passing seems all so mysterious to me. Can you guys point me in the right direction of how I should tackle this problem? Thanks
What I understand you need to select an image from list of images and pass the id. So, try this
<%= label_tag "Enter Contest" %>
<% #contest.photos.each do |photo| %>
<%= link_to (image_tag("#{some photo path}")), some_controller_method_path(:photo_id => photo.id) %>
<% end %>
And in controller you can use params[:photo_id]
I'm trying to display group name (product_group) followed by the items (product) in each group.
<% #products.group_by(&:product_group_id).each do |s|%>
<!--need to get group name here ->
<% s[1].each do |d|%>
<%= d.product_name%>
<br>
<%end%>
<%end%>
rails 2.3.8
First, I strongly recommend using two variables in your block. When you use one variable, group_by sets the variable to an array of the pair of values which should be set as two variables. It will be much more clear code than indexing the pair with [1] for the group.
One way is that the first part of the pair will be the id, so you can do a find.
<% #products.group_by(&:product_group_id).each do |product_group_id, products|%>
<!--need to get group name here -->
<%= ProductGroup.find(product_group_id).name %>
<% products.each do |product|%>
<%= product.product_name%>
<br>
<% end %>
<% end %>
Another way, is since you have an array of at least on product in the group, you can call the product_group association on the first element of the array.
<% #products.group_by(&:product_group_id).each do |product_group_id, products|%>
<!--need to get group name here -->
<%= products[0].product_group.name %>
<% products.each do |product|%>
<%= product.product_name%>
<br>
<% end %>
<% end %>
You can also delegate the name to the product group.
class Product
belongs_to :product_group
delegate :name, :to => :product_group, :prefix => true, :allow_nil => true
end
<%= products[0].product_group_name %>
Group by needs two variables in the block declaration. The first for the thing you are grouping by, and the second to hold the things in each of the groups.
<% #products.group_by(&:product_group_id).each do |group_id, group_products| %>
Group ID: <%= group_id %>
<br>
Products:
<% group_products.each do |product| %>
<%= product.product_name %>
<% end %>
<% end %>