How to use models from associations in views with HABTM - ruby-on-rails

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 %>

Related

getting message School:0x00559eba30b8b8 instead of displaying school name

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)

Rails habtm checkboxes on create

Is possible to utilize the habtm checkboxes on create action?
because this:
<%= hidden_field_tag "product[size_ids][]", nil %>
<% Size.order(:size).each do |size| %>
<li> <%= check_box_tag "product[size_ids][]", size.id, Product.size_ids.include?(size.id), id: dom_id(size) %>
<%= label_tag dom_id(size), size.size %>
</li>
<% end %>
was on update and was working since was brought to create page rails spits out the
undefined method `size_ids' for #
so, have a way to utilize the habtm on a create action?
Since you're likely dealing with one item, you probably mean:
#product.sizes_ids
The Product model doesn't have a direct association with any sizes, it's only instances of it that do.
use collection check boxes getting all of the model with ids exemple:
<%=p.collection_check_boxes :size_ids, Size.all, :id, :size %>

Rails: Elegant way to find name from related model

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 %>

Array of checkboxes in Rails

There is 'FoodType' model which are describes types of food in restaurants. I need to make view for creating a new restaurant, and I need to have list of checkboxes in order to allow user to setup types of food for each restaurant. I want to have something like this:
<% FoodType.all.each do |food_type| %>
...
<div class="row">
<%= f.check_box :food_types[0] %>
</div>
...
<% end %>
I want to have parameters like params[restaurant][food_types][0] = true in order to make some actions after creating. Please, tell me, how can I do it? Thanks in advance.
Presumably you have a join table which joins restaurants and food types? Let's say that you have one called restaurant_food_types (with a model RestaurantFoodType), which has restaurant_id and food_type_id?
You will then have this association in restaurants:
Restaurant < ActiveRecord::Base
has_many :restaurant_food_types
has_many :food_types, :through => :restaurant_food_types
This will give you the method .food_type_ids which you can call on a restaurant to set the joins. It's this method that you should hook into in your form: it expects an array of ids, so you need to set up an array-style parameter (one where the name ends in []) You may need to use check_box_tag rather than .check_box, to access an array-style parameter name: i would do this:
<% form_for #restaurant do |f| %>
<% FoodType.all.each do |food_type| %>
...
<div class="row">
<%= check_box_tag "restaurant[food_type_ids][]", food_type.id, #restaurant.food_type_ids.include?(food_type.id) %><%= food_type.name %>
</div>
...
<% end %>
<%= f.submit "Save" %>
<% end %>
Like i say i'm using a check_box_tag here but there might be a nicer way to hook into the food_type_ids method.

How can I display a list of three different Models sortable by the same :attribute in rails?

I have a Campaign model which has_many Calls, Emails, and Letters.
For now, these are each a separate Model with different controllers and actions (although I would like to start to think of ways to collapse them once the models and actions stabilize).
They do share two attributes at least: :days and :title
I would like a way to represent all the Calls, Emails, and Letters that belong_to a specific Campaign as a sortable collection (sortable by :days), in a way that outputs the model name and the path_to() for each.
For example (I know the below is not correct, but it represents the kind of output/format I've been trying to do:
#campaign_events.each do |campaign_event|
<%= campaign_event.model_name %>
<%= link_to campaign_event.title, #{model_name}_path(campaign_event) %>
end
Thanks so much. BTW, if this matters, I would then want to make the :days attribute editable_in_place.
Here is what I've got working, but want some additional insights
module CampaignsHelper
def campaign_events
return (#campaign.calls + #campaign.emails + #campaign.letters).sort{|a,b| a.days <=> b.days}
end
end
In the VIEW:
<% #campaign_events = campaign_events %>
<% #campaign_events.each do |campaign_event| %>
<% model_name = campaign_event.class.name.tableize.singularize %>
<p>
<%= link_to campaign_event.title, send("#{model_name}_path", campaign_event) %>
<%= campaign_event.days %>
</p>
<% end %>
Like this?
# controller
#campaign = Campaign.find(params[:id])
#campaign_events = (#campaign.calls + #campaign.emails + #campaign.letters).sort{|a,b| a.days <=> b.days}
# view
#campaign_events.each do |campaign_event|
<%= campaign_event.model_name %>
<%= link_to campaign_event.title, #{model_name}_path(campaign_event) %>
end
In controller you find all campaign events and sort it by days field

Resources