I am having some trouble while looping some association:
#=> member.rb
has_one :academic
#=> academic.rb
belongs_to :member
So far so good. However, when I loop through the association, I get:
undefined method 'each' for #<Academic:0x007fc98b2b7210>
Here's my view (show):
<% if !#member.academic.nil? %>
<% fields_academic = [:major, :second_major, :minor, :second_minor] %>
<h1>Academics</h1>
<% #member.academic.each do |academic| %>
<%= render 'database/shared/display', model_obj: academic, data: academic, fields: fields_academic %>
<% end %>
<% end %>
The code is stuck at the each method.
#member is defined as = Member.find(params[:id])
Any help will be gladly appreciated. Thank you in advance!
According to your code there are no multiple instances of academic related to the given member (it's defined using has_one relation).
The answer is: you cannot loop them.
The correct code for your view should be:
<% if #member.academic.present? %>
<% fields_academic = [:major, :second_major, :minor, :second_minor] %>
<h1>Academics</h1>
<%= render 'database/shared/display', model_obj: #memeber.academic, data: academic, fields: fields_academic %>
<% end %>
<% end %>
If your intention was to have many academics for a memeber, then use has_many instead.
class Member
has_many :academics
end
each should be used on a collection of records( I mean which results in a multiple records). Currently #member.academic will return a single record as it is a has_one relation between member and academic, so you can't loop with that. May be you should change your association to has_many :academics and loop it like <% #member.academics.each do |academic| %>
Method :each iterates over the block according to how this Enumerable was constructed. If no block is given, returns self. Enumerable is a mixin that provides to collections some useful methods. In your example academic is a not Enumerable, but single object. You should use has_many association.
Related
I'm trying to figure out how to list my users by a certain string. In my app I have Artist who have one Artist_profile through has_one. In my Artist_profile, I have genre, which is what I'm trying to sort them out by. I do have it working at the moment with an if statement, but it looks through every single Artist and picks out the ones with the match. For example:
<% #artists.each do |artist| if artist.artist_profile.genre == "Rock" %>
<li><%= artist.id %></li>
<% end %>
I'm trying to get it something more like this, so it's less strain on my database:
<% #artists.rock.each do |artist| %>
<li><%= artist.id %></li>
<% end %>
and my model:
def self.rock
where(Artist.artist_profile.genre == "Rock")
end
However I get this error:
undefined method `artist_profile' for #<Class:0x6962940>
I think it's something to do with the has_one method, but can't seem to get this to work no matter what I try.
You should use the Joins method. It allows you to search fields in associated models. This differs from the Includes method as the Joins method doesn't include the results in the data. For example, if you wanted to list the artist genre instead of the id, then you would want to use Includes. In your code, you should put:
#artists = Artist.joins(:artist_profile).where("artist_profiles.genre" => "Rock")
You cannot call scope as Artist.artist_profile, it's an instance method
You can defind scope rock in ArtistProfile model
The result will look like this
class ArtistProfile < ActiveRecord::Base
belongs_to :artist
scope :rock, ->{where(genre: "Rock")}
...
and use it:
#rock_profiles = ArtistProfile.rock
<% #rock_profiles.each do |profile| %>
<li><%= profile.artist_id %></li>
<% end %>
In a rails project, I use below code to retrieve data from Assignmenttable in database and show to user:
views/assignmenttables/_form.html.erb
<% task_list = Assignmenttable.find_by_current_user_id(user.id) %>
<% if task_list.blank? %>
<h1>It's blank</h1>
<% else %>
<% task_list.each do |task| %>
<%= task.id %>
<% end %>
<% end %>
data is retrive correctly from database, if task_list isn't empty this code is correct, but when task_list is empty, I get below error:
undefined method `each' for #<Assignmenttable:0x000000069541c8>
how can I handle this error?
The error you've got is caused because the object you called the .each method for is not a collection (multiple) - it's singular
Where
The way to fix this is to either use the .where method to call your data:
<%= task_list = Assignmenttable.where(user_id: current_user.id) %>
--
Associations
Or a better way will be to refactor your code to use the ActiveRecord associations which make Rails so appealing:
#app/models/user.rb
has_many :assignments
#app/models/assignment.rb # -> notice the name
belongs_to :user
current_user.assignments #-> allows you to call this in views
--
Model Names
Another thing you need to consider is your model names need to use CamelCase (AssignmentTable)
I have the following view to show the categories with the count
<% #categories.each do |category| %>
<% category.sub_categories.sort.each do |sub_category| %>
<li><%= link_to sub_category.name, "category/#{sub_category.slug}", title: sub_category.name.capitalize %> <%= sub_category.posts.where(status: 1).count %></li>
<% end %>
<% end %>
But I dont think using where in view is not good idea. Is there any other way to perform such operation.
I am getting correct count but I need better way to do this. can anyone help
Your Post model should have a scope on it that encapsulates this status logic:
class Post < ActiveRecord::Base
def self.active
where(status: 1)
end
end
Then call it like so from the view:
sub_category.posts.active.count
use scope to do the same thing, your solution is ok otherwise. you don't need to do this controller.
scope :active_posts, lambda{ where(status: 1)}
The only problem I see with this is it causes N+1 queries, because you do 1 query for the categories THEN another query for EACH category. This is ok with small quantities, however can cause serious performance problems later on.
1) I recommend you look into "counter_cache"ing:
- http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to
2) OR upon activation/deactivation of Posts, create a method that will increment/decrement a attribute on the Post's Category (i.e. "active_posts")
I think I'm overlooking something very simple here, but would really appreciate some help to work out what it is.
In the project show view, I'm displaying associated (has_many) tasks in a partial. I only want to display those records where a particular field is not empty. My view code looks like this.
<% for task in #tasks %>
<% unless task.user.notes.empty? %>
<tr>
<td><%= task.user.name %></td>
<td><%= task.user.notes %></td>
</tr>
<% end %>
<% end %>
This is returning undefined method 'notes' for nil:NilClass. This is strange as :notes is definitely in the User model.
The Project controller handling this is contains:
def show
#tasks = #project.tasks.paginate(:page => params[:page])
end
My models look as follows
Project
has_many :tasks
end
Task
belongs_to :project
belongs_to :user
end
User
has_many :tasks
end
What have I missed here? Am I using empty? correctly? Or should I be handling this in the controller? I currently have three partials on the Project show, all using the same Task query. Performance and/or best practice -wise, does it make more sense to have all three partials sourcing data from the same controller query, or to have a sperate query just for this case?
Thanks for any pointers.
The problem was that your User model was undefined when you called task.user.notes.
You can solve this problem as well as improve your overall design by making use of the #delegate macro provided by ActiveSupport in Rails. For example, inside of the Task model, try adding
delegate :notes, :to => :user, :prefix => true, :allow_nil => true
This adds a task.user_notes method to the Task model which will allow you to fetch the User's notes straight from the Task itself. Additionally, because we specified the allow_nil option, if there is no User associated with the Task, the result of the method will be nil instead of an exception.
You can also add this for the name attribute of the User allowing you to call task.user_name from within the view.
Resources:
delegate - http://apidock.com/rails/v3.0.9/Module/delegate
"Law of Demeter" - http://devblog.avdi.org/2011/07/05/demeter-its-not-just-a-good-idea-its-the-law/
In the controller
def show
#tasks = #project.tasks.paginate(
:page => params[:page],
:conditions=>["notes is not ? and notes !=?",nil,'']
)
end
OR, not in the controller
Write a helper method to abstract this.
Add a new helper method
def filter_tasks(tasks)
tasks.find(
:all,
:conditions=>["notes is not ? and notes !=?",nil,'']
)
end
And use helper in view
<% for task in filter_tasks(#tasks) %>
<% unless task.user.notes.empty? %>
<tr>
<td><%= task.user.name %></td>
<td><%= task.user.notes %></td>
</tr>
<% end %>
<% end %>
I have a country model and a places model - a country has_many places, and a place belongs_to a country. A place model also has_many posts (which belong_to a place). I would like to aggregate all the posts from places that belong to a certain country into a feed - rather like a friend activity feed on a social networking site. I'm having a bit of trouble figuring out the appropriate search to do, any help would be much appreciated! At the moment I have:
country.rb
has_many :places
has_many :posts, :through => :places
def country_feed
Post.from_places_belonging_to(self)
end
post.rb
belongs_to :place
scope :from_places_belonging_to, lambda { |country| belonging_to(country) }
private
def self.belonging_to(country)
place_ids = %(SELECT place_id FROM places WHERE country_id = :country_id)
where("place_id IN (#{place_ids})", { :country_id => country })
end
end
Then in the country show.html.erb file:
<h3>Posts</h3>
<% #country.country_feed.each do |post| %>
<%= link_to "#{post.user.username}", profile_path(post.user_id) %> posted on <%=link_to "#{post.place.name}", place_path(post.place_id) %> <%= time_ago_in_words post.created_at %> ago:
<%= simple_format post.content %>
<% end %>
This runs without any errors, but gives a feed of all the posts, rather than selecting the posts for that particular country. What's the best way to get this working? I've got a sneaking suspicion that I might be over-complicating matters...thanks in advance!
It looks like there's a syntax error in the subquery. places_id does not exist in the places table, so it really ought to read:
SELECT id FROM places WHERE country_id = :country_id
You'd think the invalid subquery would raise an exception, but it doesn't -- or else it is handled silently. Very odd.
But unless I'm missing something, you could replace all this just by using:
<% #country.posts.each do |post| %>
No?