How to iterate over multiple model results in Rails 3? - ruby-on-rails

I am not sure what I am doing wrong here. I find many examples that show that I am doing this right and it is really basic stuff, I know.
I am simplyfying a bit, but I have two models, 'Post' and 'Category'. I am trying to get the list of categories from the database and list them by name.
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
end
# get all categories and output the names
cats = Category.all
cats.each do |cat|
cat.name
end
It instead seems to output the entire array of retrieved results. All results not even just the one I am iterating over. What gives?

Where are you putting that .each loop code? Where is the "output" code you're referring to? If you're using a loop in a view, make sure you're using
<% %>
and not
<%= %>
for the loop lines themselves. As in:
<% Category.all.each do |cat| %>
<%= cat.name %>
<% end %>

Category.all returns an array of all Category objects, which is everything the categories table contains. cats is therefore an array of all the categories. I'm not sure why you think you're only iterating over "one" of anything. To get one result, you can use find() or first:
cat = Category.first
puts cat.name
If you want all the names, you can do this:
Category.all.map(&:name)
or, a bit more efficiently, especially if there are many fields:
Category.all(:select => :name).map(&:name)

Related

Doing group_by on class with belongs_to in Rails

I have a model course which has_many subcategories. I want to build a page that shows courses grouped by their subcategory. So far, I have
#courses = Course.personal_enrichment.order('subcategory_id').page params[:page]
#courses_facet = #courses.group_by(&:subcategory_id)
which works fine, but I need to show the actual subcategory name in the view, not the number. I've seen some other answers about this type of thing, but most of them assume the attribute you're grouping by is already human readable. Maybe I'm missing something?
When rendering the view you can just access the referenced models' attributes. Since group_by returns a hash, you could do something like this:
<% #courses_facet.each do |subcategory_id, courses| %>
<% subcategory_name = courses.first.subcategory.name rescue nil %>
<label><%= subcategory_name %></label>
<% end%>
Unless relevant subcategory models are cached this will generate N+1 queries to fetch the subcategory names. One way to avoid that is to include subcategory records to the initial resultset.
#courses.includes(:subcategories).group_by(&:subcategory_id)

Display multiple model results in your views in rails app

I'm adding a new model to my equasion and I'm wondering if there is a way to associate two models into one model then display any/all results within a view. For example, here is what I've currently have;
#tweet_category.order("position").each do |tweet|
<%= tweet.title %>
end
just a short example... now what if I added facebook into this. I was first thinking of creating a model thats named stuff then associate it to tweet_category and facebook_category like so;
class Stuff < ActiveRecord::Base
attr_accessible :title
belongs_to :user
has_many :tweet_category
has_many :facebook_category
end
Now in my controller I'm guessing I would do the following;
class StuffController < ApplicationController
def index
#stuff_list = Stuff.find(:all)
end
end
and in my view I would just simply do the following from above view;
#stuff_list.order("position").each do |stuff|
<%= stuff.title %>
end
am I understanding the logic here??? would that work having two models / two tables db.. etc..
First of all, I don't understand why you would need that "stuff" model. It belongs to users and has_many tweet_category and facebook_category, and just does nothing but offering a "title", when your User model could do the job ( I mean, each user could have many tweets and fb category, instead of having one or several "stuff" which has/have many of them ).
Anyway, if you want to make links between your models and then display everything in a view, first in your User model you just have to do :
class User < ActiveRecord::Base
...
has_many :facebook_categories #( I don't know how rails would pluralize it, btw, I'm just making an assumption )
has_many :tweeter_categories
end
and
class Facebook_category
...
belongs_to :user
end
and do the same fot the tweeter category
Then in your controller :
def show_everything #Here it's a custom action, but you can call it wherever you want
#users = User.all
end
And finally in your view :
<% #users.each do |user| %>
<% user.facebook_categories.all.each do |fb_c| %>
<%= fb_c.title %>
<% end %>
<% user.tweeter_categories.all.each do |t_c| %>
<%= t_c.title %>
<% end %>
<% end %>
Maybe just try to grab a better name for your models, so the pluralization doesn't get messy ( and I saw that the ".all" method is deprecated, so maybe replace it with something
Hope it helps !
Edit :
Basically, when you're doing
#users = User.all
What rails' doing is putting every hash defining every "User" in an array. So, if you want to mix two tables' arrays inside a single array, you can do something like this :
#categories = [] << Facebook_category.all, Tweeter_category.all
You will then have an array ( #category ), filled with 2 arrays ( one ActiveRecord relation for Facebook_category and one for Tweeter_category ). Themselves filled with hashes of their model. So, what you need to do is :
#categories.flatten!
Here's the API for what flatten does ( basically removing all your nested arrays inside your first tarray )
Now, you got a single array of hashes, being the informations from both your model's instances. And, if these informations can be ordered, in your view, you just have to :
<% #categories.order("updated_at").each do |i| %>
<%= i.title %>
<% end %>

Getting text to show up if a condition is true

I'm trying to get the text "Tags:" to show up only if tags are present, so I did the following in my view:
<% if #tags.present? %>
<%= puts "Tags:"%>
<% end %>
Which doesn't work... I'm a beginner, and have no idea what I'm doing wrong.
Thanks
EDIT:
A tag belongs to an Article.
Tags is defined in my Article model as:
def tag_tokens
self.tags.collect{|t| t.name}.join(", ")
end
def tag_tokens=(tags_delimited)
tags_delimited.split(",").each do |string|
self.article_tags.build(:tag => Tag.find_or_create_by_name(string.strip.downcase))
end
end
I'm trying to make it so that when an article has tags the word "Tags:" shows up before the list of tags, and when an article doesn't have any tags, the word "Tags:" doesn't show up.
Right now <% if #tags.nil %> just causes "Tags:" to show up on every post.
You don't use puts in views -- puts causes the text to go to your console. This will fix it:
<% if #tags.present? %>
<%= "Tags:"%>
<% end %>
You also don't need to use .present? by the sound of it. If you only want to see if it's been set, you should use .nil? instead. You can also condense this down to a single line.
<%= "Tags:" unless #tags.nil? %>
UPDATE: It looks like the tag_tokens method is broken for you in both the getter and setter. Your setter isn't actually saving anything by the looks of it (.build returns a new object, you need to save it). Your getter is also referencing tags, instead of article_tags which is what you're trying to save by the looks of it. Changing it to this should work for saving:
self.article_tags.build(:tag => Tag.find_or_create_by_name(string.strip.downcase)).save
This is assuming that you have a line that is something like:
has_many :article_tags
has_many :tags, through: :article_tags
Which I'm assuming you do based on your setter.
I assume this is a many-to-many relationship, but it looks like you're using has_many :through, rather than has_and_belongs_to_many. Is there a reason for this? If you're using has_and_belongs_to_many you should be able to do this:
has_and_belongs_to_many :tags
def tag_tokens=(tags_delimited)
self.tags = []
tags_delimited.split(",").each do |string|
self.tags << Tag.find_or_create_by_name(name: string)
end
end
If you do that, you should not have an ArticleTags model at all, and you should have a table called articles_tags with no primary column, and an article_id and tag_id column.
Update 2:
You're not setting #tags to anything, which is why it's always nil. #tags is a variable, which needs to be set to have a value just like #articles is being set in your controller's index method. Regardless, since this is for an index method, you wouldn't want it to be a single instance variable regardless. You should be accessing your tag_tokens method for that particular instance. app/views/articles/index.html.erb lines 53-55 should be changed to this:
<%= "Tags:" if article.tags.any? %>
Check the answer by sgrif, it contains a lot of good points. To just answer your main question:
In erb (the "language" used for view templates in Rails) you can use <%= ... %> to interpolate the result of some Ruby code into your view template.
When you are doing:
<%= puts "Tags:" %>
the following happens:
Ruby evaluates/executes your code: "Tags: " is printed to STDOUT and nil is returned since a call to puts alsways returns nil
erb interpolates the result into your template, the result is nil, which shows up as "nothing"
To fix it, just use:
<% if #tags.present? %>
<%= "Tags:"%>
<% end %>
or, since you are not doing anything in Ruby, you can just use:
<% if #tags.present? %>
Tags:
<% end %>
What has #tags been defined as? Where do you want to check if it is present?
Do you want if #tags.nil?

Ruby / Rails - .each Iterator is printing entire array at the end of the loop [duplicate]

This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 3 years ago.
I think what I'm trying to do is pretty simple, and I'm really not sure why this isn't working. I'm using Rails 3.
Essentially, I'm just trying to select the distinct values from a column in an existing model, and print them out all. For the most part, this works but the .each loop in my view also ends up printing the entire array at the end of the loop. (
I a model called Attractions, and each attraction has a Category (right now the Category is hardcoded in the DB for simplicity).
This is the Attraction Model and a class method "all_categories" defined...
class Attraction < ActiveRecord::Base
def self.all_categories
Attraction.select("DISTINCT category")
end
end
This is the Attraction Controller
class AttractionsController < ApplicationController
def index
#categories = Attraction.all_categories
#attractions = Attraction.find(:all)
end
def show
#attraction = Attraction.find(params[:id])
end
end
This is the code in my view that is causing trouble - no rocket science, just a simple iterator, ...
<%= #categories.each do |c| %>
<%= c.category %><br/>
<% end %>
Pretty simple, right? This is all running fine, BUT this is what I see when that code segment is run:
Architecture
Art
Fashion
Music
[#<Attraction category: "Architecture">, #<Attraction category: "Art">, #<Attraction category: "Fashion">, #<Attraction category: "Music">]
Why is the array at the end printed? All I want is a list of the categories:
Architecture
Art
Fashion
Music
Obviously, I'm new to Ruby/Rails, and I've tried to search all over for a solution to this. Is there something obvious that I'm missing?
Appreciate any help.
# Change this line with an =:
<%= #categories.each do |c| %>
# ...to this:
<% #categories.each do |c| %>
You only want the side effects on the block of the #each method, you don't want interpolation of the returned value.
It's because it's what happen when you do
def self.all_categories
Attraction.select("DISTINCT category")
end
It's create an Attraction Object with attribute define by your field. You can do
def self.all_categories
Attraction.select("DISTINCT category").map(&:category)
end

Rails Getting Started Question

I'm a newbie in rails, and I've been looking all over for an answer for this issue. Here's what I have:
class Item < ActiveRecord::Base
belongs_to :book
class Book < ActiveRecord::Base
has_many :items
Now I want to list all the items with their properties and the book associated with them. They would go under index in the items_controller.rb
class ItemsController < ApplicationController
# GET /items
# GET /items.xml
def index
#items = Item.all
now how would I handle books in the ItemsController so I can list them in index.html.erb keeping in mind that an item belongs to only one book? if I add:
#books = items.book.find
right under #items = Item.all so I can reference them in the index.html.erb I get:
undefined method 'book' for #<Array:0x10427f998>
I have a feeling that the answer is so simple but so far I haven't figured it out. Is there any tutorial that you guys are aware of that covers this matter?
Thank you!
In your view, when you iterate over all of your #items, just reference the book for each one. Example ERB (app/views/items/index.html.erb):
<% #items.each do |item| -%>
Item: <%= item.name %>
Book: <%= item.book.title %>
<% end -%>
If instead your intention is to display each book with the associated items under each book, you'd be better off using the index action on the BooksController. Find all the books, iterate over each book, and for each book, iterate over the items for that book.
The array of items doesn't have a book, each individual item does. It looks like you're trying to get the book of ALL the items, which doesn't exist.
Try #items[0].book or using a loop in your view:
<ul>
<% for item in #items %>
<li><%= item.book.title %></li>
<% end %>
</ul>
You could also use a partial to iterate through the array. See the section "Rendering a collection of partials" at http://api.rubyonrails.org/classes/ActionView/Partials.html
The problem is that you're calling items.book on an array of items, and the array doesn't have a method named book (hence the error). You'd need to get a single book from that array (such as items[0]), then call .book on that.

Resources