Rails 5, #child.parent, N+1 bullet - sql

Rails 5.
New app, all default.
Bullet gem tell me this:
user: root
/children/2
N+1 Query detected
Child => [:parent]
Add to your finder: :includes => [:parent]
N+1 Query method call stack
app/controllers/children_controller.rb:14:in `show'
app/controllers/children_controller.rb:14:in `show'
I have these models:
class Parent < ApplicationRecord
has_many :children
end
class Child < ApplicationRecord
belongs_to :parent
end
I have this controller in children_controller.rb with:
...
def show
#parent = #child.parent
end
...
In my views views/children/show.html.erb I have this:
...
<%= #parent.name %>
...
If I invert this and in view I put:
<%= #child.parent.name %>
and in controller:
...
def show
#nothing more
end
...
I have the same error from Bullet but in html.
How to fix this? Is really a N+1 problem or Bullet is wrong?
The project is really really new. First models.

I don't think its a N+1 problem as a child has only one parent. However, you can use includes, if that makes a difference:
#child = Child.includes(:parent).find(1)
But if you try both on terminal, you will see that ActiveRecord generates 2 SQL statements with and without the includes.

Related

Display only one field from an embedded document with MongoID

I'm a real beginner with MongoDB and MongoID.
I created two scaffolds
class Objet
include Mongoid::Document
field :nom, type: String
embeds_one :coordonnee
end
And
class Coordonnee
include Mongoid::Document
field :adresse1, type: String
field :adresse2, type: String
field :code_postal, type: String
field :ville, type: String
embedded_in :objet
end
That's what I get when creating a new Objet :
Now, I'm trying to show only the field adresse1 for this document, but it doesn't work. I can display only the whole embedded document doing this :
When I do :
<%= #objet.coordonnees.adresse1 %>
I get this error :
undefined method `adresse1' for #<Hash:0x2b2b1f0>
How can I do that ?
EDIT
Doing that, I can display all the elements "Adresse1, adresse2, ville, code_postal" :
Controller
def show
#objet = Objet.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #objet }
end
end
View
<%= #objet.nom %>
<% #objet.coordonnee.each do |t| %>
<%= t[1] %>
<% end %>
But my question is : How to display ONLY one of them ? Such as ville, or code_postal or adresse1... ?
What was your code that works for the full document? It was dropped from your post.
In the mongo Shell, you could do this with dot notation db.collection.find({},{'coordonnees.adresse1':1,'_id':0}) You need to specify the '_id':0 because _id is always returned by default.
The other answer will not work because adresse1 is a subdocument. You must include the reference to coordonnees.
Not hugely familiar with MongoID, but assuming you can make calls straight to mongo, there is a second implicit parameter to all find-like statements called a projection that specifies what exactly you would like to return.
For instance, showing only adresse1 for all items in your collection:
db.collection.find({},{"coordonnees.adresse1": 1, "_id":0})
should return only the adresse1 parameter. I wasn't quite able to tell exactly what context you're displaying the objects in, but regardless of context, api calls to mongo should be fairly straightforward to make. Let me know if I've misinterpreted this question though.
In your posted example, you should change your find function to something like the following:
Objet.find({params[:id]}, {:fields => [coordonnees.adresse1]})
Hope that helps.
I found the solution to my problem.
To display only one element of the hash, I can do :
<%= #objet.coordonnees['adresse1'] %>
I am not sure if you are using embeds_one or embeds_many as you are using singular and plural forms of the relation name interchangeably in your question.
If it is a embeds_one the problem is that you should not iterate on #objet.coordonnee as it is a single document. Your view code should look like:
<%= #objet.nom %>
<%= #objet.coordonnee.address1 %>
If it is a embeds_many, your relation name should be plural, then you should be able to use t.address1 in your view.
# model Objet
embeds_many :coordonnees
# view
<%= #objet.nom %>
<% #objet.coordonnees.each do |t| %>
<%= t.address1 %>
<% end %>

Rails simple_form checkboxes for serialized Array field

I am using SimpleForm to build my form.
I have say the following model:
class ScheduledContent < ActiveRecord::Base
belongs_to :parent
attr_accessible :lots, :of, :other, :fields
serialize :schedule, Array
end
I want to construct a form, where among many other fields and associations (this model is actually part of a has_many association already - so quite a complex form) a user is presented with a variable number of days (eg Day 1, Day 2, Day 3, etc) - and each day can be checked or unchecked. So if a user checks Day 1, and Day 5 say - I want to store [1, 5] in the schedule field. Before the form - I can construct a simple array of possible days to choose from, including obviously the days already chosen.
What is the best way to represent this form using SimpleForm's form helpers? If it is not possible to do so - I could use Rails' form helpers too to make it work, but my preference is SimpleForm as the rest of the form is already constructed using SimpleForm.
Yes, you can do it with SimpleForm. Here is an example:
<%= simple_form_for(#user) do |f| %>
<%= f.input :schedule, as: :check_boxes, collection: [['Day 1', 1], ['Day 2', 2]] %>
<%= f.button :submit %>
<% end %>
Answer to an old question, but I had to do something similar recently. To mark already-selected check box options, I used :checked similar to this:
<%=
form.input :schedule, {
as: :check_boxes,
collection: Days.my_scope.map { |day| [day.name, day.id] },
wrapper: :vertical_radio_and_checkboxes,
checked: form.object.schedule
}
%>
Was struggling with this one as well. Finally made it as the haml code below. It makes use of SimpleForm collection_check_boxes method and will output check boxes with labels vertically. List will not show general label in top for the whole checkbox list.
= f.collection_check_boxes :schedule, Day.all, :id, :label_name do |day|
= day.check_box
= day.label
%br

Key based cache expiration with Mongoid embedded documents

How would you set up a Russian doll like key based cache expiration with embedded documents?
As described by 37 signals
I believe touch was added for belongs_to in Mongoid 3.0 but how would you deal with it for embedded documents?
Example classes:
class House
embeds_many :persons
end
class Person
embedded_in :house
end
View:
<% cache ['v1', house] do %>
<%= house.some_attribute %>
<% house.persons.each |person| %>
<% cache ['v1' person] do %>
<%= render 'houses/person', person: person %>
<% end %>
<% end %>
<% end %>
What would be the simplest way to generalize the touching? So that when I update a person, the house it's embedded in gets touched.
EDIT: Or maybe the thinking here is that it's relatively cheap to re-render all the embedded items? Of course I could just do this:
class Person
after_save :touch_house
def touch_house
house.touch
end
end
I implement daisy chaining of embedded touching with observers.
class PersonObserver < Mongoid::Observer
def sweep(person)
person.house.touch
end
alias_method :after_update, :sweep
alias_method :after_create, :sweep
end
When you update or create a person, it touches that person's house effectively updating the houses' update_at time stamp.
In order to use observers, add this to your application.rb:
config.mongoid.observers = :person_observer
I define this concern:
module ParentTouchable
extend ActiveSupport::Concern
def touch_parent
self._parent.touch
end
end
and then I include it in the embedded model, so I can call touch_parent in an after_save callback. Let's say my embedded model is Comment:
class Comment
include Mongoid::Document
include ParentTouchable
after_save :touch_parent
end

Rails3 Cache Sweeper for has_and_belongs_to_many association

I have the following relationships modeled in a Rails3 application:
class User < ActiveRecord::Base
has_and_belongs_to_many :skills
end
class SkillsUser < ActiveRecord::Base
end
class Skill < ActiveRecord::Base
has_and_belongs_to_many :users
end
The "SkillsUser" model represents the many-to-many association between users and skills. In this way, when a User adds a new Skill, and said Skill already exists in the "skills" table (i.e "Java"), I simply create the relationship between the existing skill and the user in the skills_users table. All good.
Within the User's view, I display a list of Skills. And I have a fragment caching block wrapped around those skills.
<% cache([user,"skills"]) do %>
<div id="skills-grid">
<% user.sorted_skills.each do |s| %>
...
<% end %>
</div>
<% end %>
On a separate edit page, a User can add or delete a Skill. This action simply creates or removes a skills_users record. And when this happens, I need to invalidate the fragment cache so that the skills render appropriately on the User view.
So I created a CacheSweeper who's purpose in life is to observe the skills_users relationship. Here's the controller:
class SkillsController < ApplicationController
autocomplete :skill, :name
cache_sweeper :skill_user_sweeper
def create
#user = User.find(params[:user_id])
#Make sure the current user has access to
#associate a skill to the user in the request
if(#user.id = current_user.id)
SkillsHelper.associate_skill(#user,params[:skill][:name])
#skill = Skill.find_by_name(params[:skill][:name])
end
respond_to do |format|
format.js
end
end
def destroy
#skill = Skill.find_by_id(params[:id])
#user = User.find_by_id(params[:user_id])
#Destroy the relationship, not the skill
#user.skills.delete(#skill) if(#skill.can_be_tweaked_by?(current_user))
respond_to do |format|
format.js
end
end
end
And here's the sweeper:
class SkillUserSweeper < ActionController::Caching::Sweeper
observe SkillsUser
def after_create(skill_user)
expire_cache_for(skill_user)
end
def after_update(skill_user)
expire_cache_for(skill_user)
end
def after_destroy(skill_user)
expire_cache_for(skill_user)
end
private
def expire_cache_for(skill_user)
expire_fragment([skill_user.user,"skills"])
end
end
The problem is, after adding or removing a skills_users record (after "create" or "destroy" on the SkillsController), the sweeper is never invoked. I have other sweepers working within my project, but none of them observe many-to-many associations.
My question, then, is how does one create a CacheSweeper to observe a "has_and_belongs_to_many" association?
I would try using the user.id rather than user as the key. i.e. change
<% cache([user,"skills"]) do %>
to
<% cache([user.id,"skills"]) do %>
I would also add logger messages inside the callbacks as well as a logger message in the SkillUserSweeper class to make sure it is being loaded.

Rails 3: Will_paginate's .paginate doesn't work

I'm using newes Rails 3 version with will_paginate.
#videos = user.youtube_videos.sort.paginate :page => page
I also added the ##per_page attribute to my youtube_video-model.
But it just won't paginate it. I get always all items in the collection listed.
What have I done wrong?
Yours, Joern.
Why are you calling sort here? That seems unnecessary, and probably would result in it finding all videos and calling pagination on that rather than paying any attention to any variable defined in your Video model. Instead, move the sorting logic into the Video model by using a scope or use the order method.
Here's my solution, my own answer, for all other's having trouble with will_paginate and reading this issue:
Create an ApplicationController method like this:
def paginate_collection(collection, page, per_page)
page_results = WillPaginate::Collection.create(page, per_page, collection.length) do |pager|
pager.replace(collection)
end
collection = collection[(page - 1) * per_page, per_page]
yield collection, page_results
end
Then in your Controller, where you got the collection that should be paginated:
page = setup_page(params[:page]) # see below
#messages = Message.inbox(account)
paginate_collection(#messages, page, Message.per_page) do |collection, page_results|
#messages = collection
#page_results = page_results
end
And in your View:
<% #messages.each do |message| %>
<%# iterate and show message titles or whatever %>
<% end %>
<%= will_paginate #page_results %>
To get the page variable defined, check this:
def setup_page(page)
if !page.nil?
page.to_i
else
1
end
end
So page = setup_page(params[:page]) does the trick, with that simple method.
This WORKS!