ActiveModel::MissingAttributeError in rails 3.2.12 with default_scope - ruby-on-rails

We want to use default_scope to be flexible with the columns shown on customer index page. For example, if a user should not see customer phone#, then we remove phone from default_scope and want to leave the phone blank on index page. We got this idea from online and is doing a test. However there is an error missing attribute:phone when phone is selected. Here is our definition for default_scope in customer model:
def self.default_scope
Customer.scoped.select("active,name,sales_id,customer_status_category_id, short_name, zone_id, id, since_date")
end
The index page:
<table>
<tr>
<th>#</th>
<th>ShortName</th>
<th>SinceDate</th>
<th>Phone</th>
<th>Active?</th>
<th>CustomerStatusCategory</th>
<th>Sales</th>
<th>LastContactDate</th>
</tr>
<% #customers.each do |cust| %>
<tr>
<td><%= cust.id %></td>
<td><%= cust.short_name %></td>
<td><%= cust.since_date.strftime("%Y/%m/%d") %></td>
<td><%= cust.phone if cust.phone.present? %></td>
<td><%= cust.active %></td>
<td><%= cust.customer_status_category.name %></td>
<td><%= cust.sales.name %></td>
<td><%= return_last_contact_date(cust.id).strftime("%Y/%m/%d") %></td>
</tr>
<% end %>
</table>
The error is:
ActiveModel::MissingAttributeError in Customerx/customers#index
Showing C:/D/code/rails_proj/engines/customerx/app/views/customerx/customers/_index_partial.html.erb where line #32 raised:
missing attribute: phone
Is it possible to show an empty column by purposely not selecting it in index (or any view page)? If it is possible, is default_scope a right tool to accomplish that? Thanks.

Related

Rails: Sum column by group

I created this code that pulls the information I need.
def index
#votes = Vote.all
#originalitysum = Vote.group(:widget_id).sum(:originality)
end
It returns a hash:
{188=>5, 160=>2}
I now need to match the key to the widget_id and return the value. I.E:
If the widget_id is 188 return 5.
<% #votes.group(:widget_id).each do |vote| %>
<tr>
<td><%= vote.widget.name %></td>
<td><%= vote.widget.store %></td>
<td><%= %></td> <!-- This needs to be the total Originality -->
<td><%= vote.interest %></td>
<td><%= vote.rating %></td>
</tr>
<% end %>
I'm open to changing this if some other way makes more sense.
You can get the originality sum with #originalitysum[vote.widget.id]
Figured it out.
Controller
def index
#votes = Vote.all
#originalitysum = Vote.select([:widget_id, :originality]).group('widget_id').sum(:originality)
#votecount = Vote.group(:widget_id).count(:originality)
#votes = Vote.select(:widget_id,
"SUM(originality) as originality_sum",
"SUM(interest) as interest_sum",
"SUM(rating) as rating_sum").group(:widget_id).order("rating_sum DESC")
end
The view
<% #votes.group(:burger_id).each do |vote| %>
<tr>
<td><%= vote.widget.name %></td>
<td><%= vote.widget.store %></td>
<td><%= vote.originality_sum %></td>
<td><%= vote.interest_sum %></td>
<td><%= vote.rating_sum %></td>
</tr>
<% end %>
Thanks to this answer in this link, I was able to parse it together.
Group by a column and then get the sum of another column in Rails
The added bonus is that it allowed me to easily sum the other columns as well.

Rails: `undefined method `per_code' for nil:NilClass` When Chaining Info From Different Object Models

I'm creating an admin page for my app in which there is a table that shows user data. Each user has completed several quizzes. In each quiz (per_quiz, bal_quiz, etc.) there is a code that needs to be reported on this page (the columns are per_code in the per_quiz model, bal_code in the bal_quiz model, etc.
Each user has_one bal_quiz (and per_quiz and so on) and each per_quiz belongs_to a user.
I'm trying to call it like this:
<% #users.each do |u| %>
<tr>
<td><%= u.id %></td>
<td><%= u.first_name %></td>
<td><%= u.last_name %></td>
<td><%= u.per_quiz.per_code %></td>
<td><%= u.bal_quiz.bal_code %> (Balance)
<br><%= u.flex_quiz.flex_code %> (Flexibility)
</td>
</tr>
<% end %>
In my controller I have #users = User.order("last_name DESC").
The error I am getting is undefined method 'per_code' for nil:NilClass called on the <td> line of the view.
Can anyone help me figure out why this isn't working? I have similar erb elsewhere in the app and this syntax works...
It's due to u.per_quiz equals to nil. Use try to solve this problem:
<td><%= u.try(:per_quiz).try(:per_code) %></td>

undefined method `ncbi_ref_seq' for #<ActiveRecord::Associations::CollectionProxy []>

Generator has_many:results
Result belongs_to:generator
I want to have a page whereby i can view all the generators and the results.
For example :This is from my generator/index.html
this is from result/index.html
what i want to have is to combine them together and view all the data.
right now this is what i've changed but i get the error message undefined method for ncbi_ref_seq. ( ncbi_ref_seq is an attribute belonging to the class Result )
GeneratorController.rb
def index
#generators = Generator.all(:include => [:results])
end
Generator/index.html.erb
<tbody>
<% #generators.each do |generator| %>
<tr>
<td><%= generator.primer_length %></td>
<td><%= generator.choice %></td>
<td><%= generator.random_primer_generated %></td>
<td><%= generator.c_primer %></td>
<td><%= generator.results.ncbi_ref_seq %></td>
</tr>
<% end %>
Since a generator has many results. When you take generator.results it returns an Active Record Collection, so there would be several records of Results, so you can't just extra a ncbi_ref_seq.
Either you have to loop through generator.results and output each ncbi_ref_seq like so
<% for result in generator.results %>
<%= result.ncbi_ref_seq %>
<% end %>
Or a generator has_one result.

Compute multiple distinct averages

I'm fairly new to rails, and am still getting used to putting together methods. I'm currently trying to create a method that averages distinct data from multiple columns. I'd like to do it all in one method so that I can display the information easily in an html table.
Currently I have this in my model:
def averagedonate
scores.group(:donatedate).average('donateamount')
scores.group(:donatedate).average('rating')
end
I'd like to be able to use them in a table like this:
<% #averagedonate.each do |donatedate, donateamount, rating| %>
<tr>
<td><%= donatedate %></td>
<td><%= donateamount %></td>
<td><%= rating %></td>
</tr>
How do I change my averagedonate method to do this? Thanks in advance!
I haven't tested, but something to this effect should work
def averagedonate
scores.select("
AVG(donateamount) as avg_donateamount,
AVG(rating) as avg_rating,
donatedate
")
.group(:donatedate)
end
Then use it like this
<% #averagedonate.each do |item| %>
<tr>
<td><%= item.donatedate %></td>
<td><%= item.avg_donateamount %></td>
<td><%= item.avg_rating %></td>
</tr>
<% end %>

How do I output name instead of ID?

Rails newbie here! I have a has_many_and_belongs_to_many relationship between items and builds.
In the code provided below, the ID of the items are being displayed instead of the name of the item with that ID. How do I display the name of the item?
<% current_user.builds.each do |build| %>
<tr>
<td><%= build.hero.name%></td>
<td><%= build.user.email%></td>
<td><%= build.name %></td>
<td><%= build.starting_items %></td>
<td><%= build.early_items %></td>
<td><%= build.core_items %></td>
<td><%= build.situational_items %></td>
</tr>
<% end %>
If you mapped association (has_one) in model with Hero and User, then probably you can use following code:
build.hero.field_name # Where field_name is column name from Hero Model
Please let me know if this will work for you or not.

Resources