I am trying to show a column of a related table with ng-repeat, but I can't.
I have 3 tables:
create_table "events", force: :cascade do |t|
t.string "title"
t.text "description"
t.time "hour"
t.integer "duration"
t.date "date"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
t.string "formatted_addres"
t.integer "location_id"
end
create_table "taggings", force: :cascade do |t|
t.integer "event_id"
t.integer "tag_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "taggings", ["event_id"], name: "index_taggings_on_event_id"
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id"
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "tags", ["name"], name: "index_tags_on_name"
and I am using ng-repeat to show the events
controller code:
app.controller('eventController', ['$http', function($http){
var store = this;
store.events = [];
$http.get('/events.json').success(function(data){
store.events = data;
});
}]);
The html code:
<section ng-repeat="event in eventShow.events | orderBy:'date'">
<div class="panel panel-default single-event">
<div class="panel-heading">
<h3>
{{event.title}}
<em class="pull-right">Created by user: {{event.user_id}}</em>
</h3>
</div>
<div class="panel-body">
Description:<br>{{event.description}} <br>
<em class="pull-right">{{event.date | date:'dd-MM-yyyy' }} - {{event.hour | date:"H:mm"}}</em> <br>
{{event.duration}} minutes <br>
{{ event.tags }}
</div>
</div>
</section>
Everything works, but when I try tho show the tags on the ng-repeat like {{ event.tags.name }} it doesn't show anything.
In rails works a similar sintaxis, but I see in angular it must be done in a different way. I was searching information about it yesterday and today and I did not find anything. Anybody can help? Thanks!
This is the data of my controller:
# GET /events
# GET /events.json
def index
#events = Event.all
binding.pry
respond_to do |format|
format.html { render :index }
format.json { render json: #events }
end
end
If I do a binding.pry I can do #events.first.tags and I see al tags of this event, so in ruby code I can do
<ul>
<% #events.each do |event| %>
<li><%= event.tags %></li>
<% end %>
</ul>
And this is what I can't do with angular {{ event.tags }} the json response that I get from get /events.json is like this
[
{
"id": 18,
"title": "awesome event",
"description": "incredible description",
"hour": "2000-01-01T11:12:00.000Z",
"duration": 12,
"date": "2015-11-25",
"user_id": 1,
"created_at": "2015-11-26T11:57:03.433Z",
"updated_at": "2015-11-26T11:57:03.433Z",
"latitude": 42.5767239,
"longitude": 3.4435419,
"formatted_addres": "somewhere",
"location_id": null
}
]
and if I use the debugger, obviously I only see the fields that are in the json with the ng-repat. But I need to list the items in the column of another table ralated like I do in rails.
I'm using:
Rails 4.2.5
Ruby 2.2.1p85 (2015-02-26 revision 49769)
please be patient with me I'm sill learning angular
Try by change controller code:
app.controller('eventController', ['$scope', '$http', function($scope, $http){
$http.get('/events.json').success(function(data){
$scope.events = data;
});
}]);
Change your html with events scope
<section ng-repeat="event in events | orderBy:'date'">
for your reference about $scope in angularjs
Update:
create file => app/views/events/index.json.jbuilder
json.array! #events do |event|
json.(event, :id, :title, :description, :hour, :duration, :date, :user_id, :latitude, :longitude, :formatted_addres, :location_id)
json.tags event.tags
end
# change in controller:
def index
#events = Event.all
binding.pry
respond_to do |format|
format.html
format.json
end
end
In order to bind some variable in UI you need to bind it to $scope first. So try using $scope.store instead var store.
Inject $scope in dependencies.
Related
I want to save a record to a Bookings table that has foreign keys from Schedules and Users table.
Here is the schema:
create_table "bookings", force: :cascade do |t|
t.integer "seats"
t.integer "base_price"
t.integer "total_price"
t.string "status"
t.integer "schedules_id", null: false
t.integer "users_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["schedules_id"], name: "index_bookings_on_schedules_id"
t.index ["users_id"], name: "index_bookings_on_users_id"
end
create_table "schedules", force: :cascade do |t|
t.string "departure", null: false
t.string "destination", null: false
t.integer "seats_available", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "price"
t.datetime "date_time"
end
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "password_digest", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "firstname", null: false
t.string "lastname", null: false
end
Schedule and User has_many :bookings and Booking belongs_to :schedules and :users.
My view in bookings/new.html.erb
<%= form_for(#book, url: booking_path, local: true) do |f| %>
<% #books.each do |data| %>
<%= data.departure %>
<%= data.destination %>
<%= data.formatted_date_time %>
<%= data.seats_available %>
<%= data.price %>
<%= f.hidden_field :base_price, value: data.price %>
<%= f.hidden_field :seats %>
<%= f.hidden_field :total_price %>
<%= f.submit "Book" %>
<% end %>
<% end %>
I'm not sure this is the best way to do it but this is my attempt. Assume the hidden fields has a way to get the data, because I cut out the code that does it since I can see the params being passed in the Command Prompt.
My routes:
get 'booking', to: 'bookings#new'
post 'booking', to: 'bookings#create'
My BookingsController:
def new
#books = Schedule.where(id: params[:book_id])
#book = Booking.new
end
def create
#book = Booking.new(booking_params)
if #book.save
flash[:success] = 'success'
redirect_to root_path
end
end
private
def booking_params
params.require(:booking).permit(:base_price, :seats, :total_price)
end
In the command prompt it does display the parameters { "booking"=>{"base_price"=>"80", "seats"=>"2", "total_price"=>"160"}, "commit"=>"Book"}. The problem is that after that it now loads bookings/create.html.erb without saving the params. It somehow doesn't do the if #book.save part. Also I don't know how I can put the foreign keys or the :status variable or do I even need to. I appreciate the help!
I am very new to rails and active record so I'm probably doing something wrong, but... using the schema below, I want to create a list of courses in the outer do loop, then list each student enrolled in the course with the inner do loop. I m not sure if this approach will work, but this error keeps popping up:
ActionView::Template::Error (undefined method `enrollments' for #):
it seems that the association is made. what am i doing wrong?
Professor show page:
<div class="col-md-8">
<h2 class="card-title"><%= #professor.name %></h2>
<% #courses_taught.each do |course| %>
<div class="card mb-4 card-header">
<img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
<h3 class="card-text"><%= course.title %></h3>
</div>
<div class="card-body">
<% course.sections.enrollments.students.each do |student| %>
<p><% student.name %></p>
<% end %>
</div>
<% end %>
</div>
models:
enrollment
class Enrollment < ApplicationRecord
belongs_to :section
belongs_to :student
end
Student:
class Student < ApplicationRecord
has_many :enrollments
end
Professor:
class Section < ApplicationRecord
has_many :enrollments
belongs_to :professor
belongs_to :course
validates_uniqueness_of :professor_id, scope: :course_id
scope :by_professor_id, ->(prof_id) { where('professor_id = ?', prof_id) }
end
Course:
class Course < ApplicationRecord
enum status: { planning: 0, offered: 1 }
scope :offered, -> { where(status: 1) }
scope :planning, -> { where(status: 0) }
belongs_to :department
has_many :sections
has_many :professors, through: :sections
validates :title, :number, :status, :description, presence: true
validates :description, length: { in: 10..500 }
validates :title, :number, uniqueness: { case_sensitive: false }
def self.search(term)
if term
where('title LIKE ?', "%#{term}%").order('title DESC')
else
order('title ASC')
end
end
def self.taught_by(professor_id)
Course
.joins(:sections)
.joins(:professors)
.where(sections: { professor_id: professor_id })
.select('distinct courses.*')
end
end
Schema:
ActiveRecord::Schema.define(version: 20171013201907) do
create_table "courses", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 0
t.integer "department_id"
t.index ["department_id"], name: "index_courses_on_department_id"
end
create_table "departments", force: :cascade do |t|
t.string "name"
t.text "description"
t.text "main_image"
t.text "thumb_image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "enrollments", force: :cascade do |t|
t.integer "section_id"
t.integer "student_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["section_id"], name: "index_enrollments_on_section_id"
t.index ["student_id"], name: "index_enrollments_on_student_id"
end
create_table "professors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "status", default: 0
t.integer "department_id"
t.text "bio"
t.index ["department_id"], name: "index_professors_on_department_id"
end
create_table "sections", force: :cascade do |t|
t.integer "number"
t.integer "max_enrollment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "professor_id"
t.integer "course_id"
t.string "room"
t.index ["course_id"], name: "index_sections_on_course_id"
t.index ["professor_id", "course_id"], name: "index_sections_on_professor_id_and_course_id", unique: true
t.index ["professor_id"], name: "index_sections_on_professor_id"
end
create_table "students", force: :cascade do |t|
t.string "name"
t.decimal "gpa"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "name"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "roles"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
enrollment belongs_to user, there is no need to use each on single record rather it will throw error. You can use below code.
<% course.sections.each do |section| %>
<% section.enrollments.each do |enrollment| %>
<p><% enrollment.student.name %></p>
<% end %>
<% end %>
Sean's answer is almost correct but since enrollments belongs_to student
you would use the singular form enrollment.student. but more simply you can just call student from inside enrollments block.
<div class="col-md-8">
<h2 class="card-title"><%= #professor.name %></h2>
<% #courses_taught.each do |course| %>
<div class="card mb-4 card-header">
<img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
<h3 class="card-text"><%= course.title %></h3>
</div>
<div class="card-body">
<% course.sections.each do |section| %>
<% section.enrollments.each do |enrollment| %>
<p><% enrollment.student.name %></p>
<% end %>
<% end %>
</div>
<% end %>
</div>
<div class="col-md-8">
<h2 class="card-title"><%= #professor.name %></h2>
<% #courses_taught.each do |course| %>
<div class="card mb-4 card-header">
<img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
<h3 class="card-text"><%= course.title %></h3>
</div>
<div class="card-body">
<% course.sections.each do |section| %>
<% section.enrollments.each do |enrollment| %>
<p><% enrollment.student.name %></p>
<% end %>
<% end %>
</div>
<% end %>
</div>
I am trying to make a association between Printlist and Product, Template and Paper Type, but I'm getting an error. When I submit the new form, it redirects to the index route, but ramains at the same new page and do not save the info to the DB.
Controller:
class PrintlistsController < ApplicationController
before_action :set_printlist, only: [:show, :edit, :update, :destroy]
def index
#printlists = Printlist.all
#preco = calcular_preco(Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id),Printlist.count(:id))
end
def show
end
def new
#printlist = Printlist.new
end
def edit
end
def create
#binding.pry
#product = Product.where('codref': product_params['codref'])
#template = Poster.find(layout_params['id'])
#size = Cartaze.find(cartaz_params['id'])
#if product.nil?
# product = Product.new
# product.codref = product_params['codref']
# product.title = product_params['title']
# product.tipo = product_params['tipo']
# product.size = product_params['size']
# product.save
#end
#printlist = Printlist.new(printlist_params)
##printlist.product = product
##printlist.template = template
##printlist.size = size
respond_to do |format|
if #printlist.save
format.html { redirect_to printlists_path , notice: 'Impression was successfully created.' }
format.json { render :show, status: :created, location: #printlist }
else
format.html{ render :new }
format.json{ render json: #printlistst.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #printlist.update(printlist_params)
format.html { render action: "edit" }
format.json { render :show, status: :ok, location: #printlist }
else
format.html { render :edit }
format.json { render json: #printlist.errors, status: :unprocessable_entity }
end
end
end
def destroy
#printlist = Printlist.find(params[:id])
#printlist.destroy
respond_to do |format|
format.html { redirect_to(Printlists_path) }
format.xml { head :ok }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_printlist
#printlist = Printlist.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def printlist_params
params.require(:printlist).permit(:product_id, :poster_id, :cartaze_id)
end
def calcular_preco(na0, na1, na2, na3, na4, na5, na6)
#formulando preco
#precos = {a0: 1, a1: 2, a2: 3, a3: 4, a4: 5, a5: 6, a6: 7}
#cartaz = Cartaze.all
pr = HashWithIndifferentAccess.new(#cartaz.map{ |c| [c.size.to_s,c.price.to_f] }.to_h)
#precos = HashWithIndifferentAccess.new({a0: 1, a1: 2, a2: 3, a3: 4, a4: 5, a5: 6, a6: 7})
#preco_total = (na0 * pr[:A0]) + (na1 * pr[:A1]) + (na2 * pr[:A2]) + (na3 * pr[:A3]) + (na4 * pr[:A4]) + (na5 * pr[:A5]) + (na6 * pr[:A6])
end
#def product_params
#params.require(:product_id).permit(:product_id)
#end
#def poster_params
# params.require(:poster).permit(:id)
#end
#def cartaze_params
# params.require(:cartaze).permit(:id, :size)
#end
end
new.html.erb.rb form:
<%= render "/partials/sidebar" %>
<div class="conteudo-dash">
<div class="linha-dash">
<div class="panel panel-defaul col-md-6">
<div class="panel-body">
<h2>Cadastro de Cartaz</h2>
<%= form_for(#printlist, url: printlists_path) do |f|%>
<%= f.collection_select(:product_id, Product.all, :id, :title,{}, {class: "form-control col-md-3"}) %>
<%= f.collection_select(:poster_id, Poster.all, :id, :name,{},{class: "form-control"}) %>
<%= f.collection_select(:cartaze_id, Cartaze.all, :id, :size, {},{class: "form-control"}) %><br>
<%= f.submit "Salvar Produto", class: "btn btn-success" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
printlist.rb model:
class Printlist < ApplicationRecord
include ActiveModel::ForbiddenAttributesProtection
belongs_to :posters
belongs_to :cartazes
belongs_to :products
end
schema.rb:
ActiveRecord::Schema.define(version: 20170523202905) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "cartazes", force: :cascade do |t|
t.string "product"
t.decimal "price", precision: 7, scale: 2
t.string "tipo"
t.string "size"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "dashboards", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "products_id"
t.integer "posters_id"
t.integer "cartazes_id"
t.index ["cartazes_id"], name: "index_dashboards_on_cartazes_id", using: :btree
t.index ["posters_id"], name: "index_dashboards_on_posters_id", using: :btree
t.index ["products_id"], name: "index_dashboards_on_products_id", using: :btree
end
create_table "dashboards_posters", id: false, force: :cascade do |t|
t.integer "dashboard_id", null: false
t.integer "poster_id", null: false
t.index ["dashboard_id", "poster_id"], name: "index_dashboards_posters_on_dashboard_id_and_poster_id", using: :btree
end
create_table "posters", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "printlists", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "product_id"
t.integer "poster_id"
t.integer "cartaze_id"
t.index ["cartaze_id"], name: "index_printlists_on_cartaze_id", using: :btree
t.index ["poster_id"], name: "index_printlists_on_poster_id", using: :btree
t.index ["product_id"], name: "index_printlists_on_product_id", using: :btree
end
create_table "products", force: :cascade do |t|
t.string "title"
t.decimal "price"
t.string "tipo"
t.string "size"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.decimal "preco_promo", precision: 7, scale: 2
t.string "codref"
end
create_table "servicos", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
add_foreign_key "dashboards", "cartazes", column: "cartazes_id"
add_foreign_key "dashboards", "posters", column: "posters_id"
add_foreign_key "dashboards", "products", column: "products_id"
end
EDIT
routes.rb
Rails.application.routes.draw do
get 'notification/create'
resources :products
devise_for :users
root 'home#index'
# user_root_path 'dashboard#index' # DEPOIS DO CLIENTE ESTAR LOGADO redireciona pra dashboard
post 'pgtcheckout/create'
post 'notification', to: 'notification#create'
resources :users
get 'pgtcheckout' => 'pgtcheckout#index'
get 'pgtcheckout/new' => 'pgtcheckout#new'
resources :dashboards
resources :product
resources :posters
resources :printlists
Console Output
Started POST "/printlists" for ::1 at 2017-05-29 13:53:04 -0300
Processing by PrintlistsController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"KrKq5k71/exf2nnw56F43IbbZz7CRdjnsvTSulSNAM9Uj8lPB8tnQ9BfDifeXlMLVTNqIs6/T1dSeIS5zeph
sQ==", "printlist"=>{"product_id"=>"7", "poster_id"=>"4", "cartaze_id"=>"3"}, "commit"=>"Salvar Produto"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1
]]
(0.0ms) BEGIN
(0.0ms) ROLLBACK
Rendering printlists/new.html.erb within layouts/application
Rendered partials/_sidebar.html.erb (12.0ms)
Product Load (1.0ms) SELECT "products".* FROM "products"
Poster Load (0.0ms) SELECT "posters".* FROM "posters"
Cartaze Load (0.0ms) SELECT "cartazes".* FROM "cartazes"
Rendered printlists/new.html.erb within layouts/application (52.0ms)
Completed 200 OK in 1117ms (Views: 1106.2ms | ActiveRecord: 2.0ms)
Debug Output:
--- !ruby/object:ActionController::Parameters
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: "✓"
authenticity_token: KrKq5k71/exf2nnw56F43IbbZz7CRdjnsvTSulSNAM9Uj8lPB8tnQ9BfDifeXlMLVTNqIs6/T1dSeIS5zephsQ==
printlist: !ruby/object:ActionController::Parameters
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
product_id: '1'
poster_id: '3'
cartaze_id: '3'
permitted: false
commit: Salvar Produto
The new Printlist object is failing a validation when executing #printlist.save on create action; that's why you get a (0.0ms) ROLLBACK in your logs.
So, #printlist.save returns false and executes format.html { render :new }; as instructed in create action:
if #printlist.save
format.html { redirect_to printlists_path , notice: 'Impression was successfully created.' }
format.json { render :show, status: :created, location: #printlist }
else
format.html{ render :new }
format.json{ render json: #printlistst.errors, status: :unprocessable_entity }
end
To spot the error, use save! instad of save (this is only temporary), that will raise an exception describing why the object is not valid, and thus not saving.
With save! in place, now you will get an exception (instead of a ROLLBACK) in your logs with the error message.
Once you spot why your validation fails, you can fix it; and don't forget to change back save! to save.
I am using rails 4, and I want to show monthly ticket generated through pie charts. Can anybody help?
Schema:
create_table "tickets", force: :cascade do |t|
t.integer "store_id"
t.integer "vendor_id"
t.datetime "ticket_date"
t.datetime "deadline"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "ticket_no"
t.string "vendor_name"
t.string "user_id"
t.integer "issue_id"
t.integer "category_id"
t.boolean "completed", default: false
end
My ticket.rb contains:
def self.category_count(user,category)
if user.vendor.nil?
category.tickets.where('store_id = ?', user.store.id)
else
category.tickets.where('vendor_id = ?', user.vendor.id)
end
end
def self.ticket_count(user,ticket)
if user.vendor.nil?
ticket.tickets.where('store_id = ?', user.store.id)
else
ticket.tickets.where('vendor_id = ?', user.vendor.id)
end
end
view:
tickets:
index.html.erb file is:
<% data = {} %>
<% for category in Category.all %>
<% data[:"#{category.name}"] = Ticket.category_count(current_user,category).count %>
<% end %>
<%= pie_chart(data) %>
I would probably start by moving this into a view helper method such as:
def pie_chart_data(user)
Hash[*Category.all.map { |c| [c.name, Ticket.category_count(user, c).count] }]
end
You could then call this in your view
<%= pie_chart(pie_chart_data(current_user)) %>
The nested attribute of quantifieds are results. In the results partial a User will checkoff if their quantified result is a :good thing or not.
In the sidebar I want to .count how many good results the User has marked off to serve as a reference point of their success.
SIDEBAR SECTION: layouts/_count.html.erb
<div class="stats">
<a href="<%= following_user_path(#user) %>">
<strong id="following" class="stat">
<%= #user.quantifieds.count %> #Works
</strong>
Quantified
</a>
<a href="<%= followers_user_path(#user) %>">
<strong id="followers" class="stat">
<%= #user.results.good.count %> #Remains zero regardless of number of checked :good boxes
</strong>
Good
</a>
</div>
quantifieds/_result_fields.html.erb
<div class="nested-fields">
<div class="form-group">
<%= f.text_field :result_value, class: 'form-control', placeholder: 'Enter Result' %>
<br/>
<%= f.date_select :date_value, :order => [:month, :day, :year], :with_css_classes => true, :class => "modular-date-field" %>
<b><%= link_to_remove_association "Remove Result", f %></b>
<div class="america3">
<label> Good: </label>
<%= f.check_box :good %>
</div>
</div>
</div>
result.rb
class Result < ActiveRecord::Base
belongs_to :user
belongs_to :quantified
default_scope { order('date_value DESC') }
scope :good, -> { where(good: true) }
scope :bad, -> { where(good: false) }
end
Should we add a method to the application controller?
class ApplicationController < ActionController::Base
before_action :load_todays_habits
before_action :set_top_3_goals
before_action :randomize_value
before_action :set_stats
protect_from_forgery with: :exception
include SessionsHelper
def set_top_3_goals
#top_3_goals = current_user.goals.unaccomplished.top_3 if current_user
end
def randomize_value
#sidebarvaluations = current_user.valuations.randomize if current_user
end
def set_stats
#quantifieds = Quantified.joins(:results).all
#averaged_quantifieds = current_user.quantifieds.averaged if current_user
#instance_quantifieds = current_user.quantifieds.instance if current_user
end
private
def load_todays_habits
#user_tags = current_user.habits.committed_for_today.tag_counts if current_user
#all_tags = Habit.committed_for_today.tag_counts if current_user
end
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end
class QuantifiedsController < ApplicationController
before_action :set_quantified, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :destroy]
def index
if params[:tag]
#quantifieds = Quantified.tagged_with(params[:tag])
else
#quantifieds = Quantified.joins(:results).all
#averaged_quantifieds = current_user.quantifieds.averaged
#instance_quantifieds = current_user.quantifieds.instance
end
end
def show
end
def new
#quantified = current_user.quantifieds.build
end
def edit
end
def create
#quantified = current_user.quantifieds.build(quantified_params)
if #quantified.save
redirect_to quantifieds_url, notice: 'Quantified was successfully created'
else
#feed_items = []
render 'pages/home'
end
end
def update
if #quantified.update(quantified_params)
redirect_to quantifieds_url, notice: 'Goal was successfully updated'
else
render action: 'edit'
end
end
def destroy
#quantified.destroy
redirect_to quantifieds_url
end
private
def set_quantified
#quantified = Quantified.find(params[:id])
end
def correct_user
#quantified = current_user.quantifieds.find_by(id: params[:id])
redirect_to quantifieds_path, notice: "Not authorized to edit this goal" if #quantified.nil?
end
def quantified_params
params.require(:quantified).permit(:categories, :metric, :result, :date, :comment, :private_submit, :tag_list, :good, results_attributes: [:id, :result_value, :date_value, :good, :_destroy])
end
end
quantifieds/_form
<%= javascript_include_tag "quantified.js" %>
<%= simple_form_for(#quantified) do |f| %>
<%= f.error_notification %>
<div class="america">
<form>
<% Quantified::CATEGORIES.each do |c| %>
<%= f.radio_button(:categories, c, :class => "date-format-switcher") %>
<%= label(c, c) %>
<% end %>
<br/>
<br/>
<div class="form-group">
<%= f.text_field :tag_list, quantified: #quantified.tag_list.to_s.titleize, class: 'form-control', placeholder: 'Enter Action' %>
</div>
<div class="form-group">
<%= f.text_field :metric, class: 'form-control', placeholder: 'Enter Metric' %>
</div>
<div id="results">
<%= f.fields_for :results do |result| %>
<%= render 'result_fields', :f => result %>
<% end %>
</div>
<div class="links">
<b><%= link_to_add_association 'Add Result', f, :results %></b>
</div>
<div class="america2">
<%= button_tag(type: 'submit', class: "btn") do %>
<span class="glyphicon glyphicon-plus"></span>
<% end %>
<%= link_to quantifieds_path, class: 'btn' do %>
<span class="glyphicon glyphicon-chevron-left"></span>
<% end %>
<%= link_to #quantified, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
</div>
<label> Private: </label>
<%= f.check_box :private_submit %>
</form>
</div>
<% end %>
quantifieds.rb
class Quantified < ActiveRecord::Base
belongs_to :user
has_many :results #correct
has_many :comments, as: :commentable
accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true #correct
scope :averaged, -> { where(categories: 'Averaged') }
scope :instance, -> { where(categories: 'Instance') }
scope :private_submit, -> { where(private_submit: true) }
scope :public_submit, -> { where(private_submit: false) }
validates :categories, :metric, presence: true
acts_as_taggable
CATEGORIES = ['Averaged', 'Instance']
end
schema.rb
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150317171422) do
create_table "activities", force: true do |t|
t.integer "trackable_id"
t.string "trackable_type"
t.integer "owner_id"
t.string "owner_type"
t.string "key"
t.text "parameters"
t.integer "recipient_id"
t.string "recipient_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "activities", ["owner_id", "owner_type"], name: "index_activities_on_owner_id_and_owner_type"
add_index "activities", ["recipient_id", "recipient_type"], name: "index_activities_on_recipient_id_and_recipient_type"
add_index "activities", ["trackable_id", "trackable_type"], name: "index_activities_on_trackable_id_and_trackable_type"
create_table "comments", force: true do |t|
t.text "content"
t.integer "commentable_id"
t.string "commentable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type"
create_table "days", force: true do |t|
t.integer "level_id"
t.integer "habit_id"
t.boolean "missed", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "goals", force: true do |t|
t.string "name"
t.date "deadline"
t.boolean "accomplished"
t.boolean "private_submit"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "goals", ["user_id", "created_at"], name: "index_goals_on_user_id_and_created_at"
add_index "goals", ["user_id"], name: "index_goals_on_user_id"
create_table "habits", force: true do |t|
t.datetime "left"
t.integer "level"
t.text "committed"
t.datetime "date_started"
t.string "trigger"
t.string "target"
t.string "reward"
t.boolean "private_submit"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "habits", ["user_id", "created_at"], name: "index_habits_on_user_id_and_created_at"
add_index "habits", ["user_id"], name: "index_habits_on_user_id"
create_table "levels", force: true do |t|
t.integer "user_id"
t.integer "habit_id"
t.boolean "passed", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "quantifieds", force: true do |t|
t.string "categories"
t.string "metric"
t.boolean "private_submit"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "quantifieds", ["user_id", "created_at"], name: "index_quantifieds_on_user_id_and_created_at"
add_index "quantifieds", ["user_id"], name: "index_quantifieds_on_user_id"
create_table "relationships", force: true do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id"
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id"
create_table "results", force: true do |t|
t.integer "user_id"
t.string "result_value"
t.date "date_value"
t.integer "quantified_id"
t.boolean "good"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.text "missed_days"
t.text "missed_levels"
t.string "provider"
t.string "uid"
t.string "oauth_token"
t.datetime "oauth_expires_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
create_table "valuations", force: true do |t|
t.string "name"
t.boolean "private_submit"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "valuations", ["user_id", "created_at"], name: "index_valuations_on_user_id_and_created_at"
add_index "valuations", ["user_id"], name: "index_valuations_on_user_id"
end
class User < ActiveRecord::Base
has_many :authentications
has_many :habits, dependent: :destroy
has_many :levels
has_many :valuations, dependent: :destroy
has_many :comments, as: :commentable
has_many :goals, dependent: :destroy
has_many :quantifieds, dependent: :destroy
has_many :results, dependent: :destroy
Thanks so much for your time!
You can update your Result as follows:
class Result < ActiveRecord::Base
# rest of the code
scope :good, -> { where(good: true) }
scope :good_count, -> { good.count }
end
Let's perform some tests in rails console:
u = User.create({ user_attributes })
u.results.create(good: true)
u.results.create(good: false)
u.results.create(good: true)
u.results.count
# => SELECT COUNT(*) FROM "results" WHERE "results"."user_id" = ? [["user_id", 1]]
# => 3
u.results.good_count
# => SELECT COUNT(*) FROM "results" WHERE "results"."user_id" = ? AND "results"."good" = 't' [["user_id", 1]]
# => 2
If you changed your User as follows:
class User < ActiveRecord::Base
# rest of the code
def good_results_count
results.good_count
end
end
which is much cleaner solution, you can use it in your application like:
# assuming we have data set like in previous step
u = User.last
u.good_results_count # you can use this line in your template
# => SELECT COUNT(*) FROM "results" WHERE "results"."user_id" = ? AND "results"."good" = 't' [["user_id", 1]]
# => 2
If, for some reason, the value remains 0 - as you mentioned - try performing the operations I described in console to see real data in database. Maybe this is not the problem with fetching proper data, maybe data is not saved correctly?
Let me know how it goes, so we're try to address the real problem!
Good luck!
Update
Problem in view is due to lack of proper association between User and Result. What currently is in User:
class User < ActiveRecord::Base
# rest of the code
has_many :quantifieds, dependent: :destroy
has_many :results, dependent: :destroy
# rest of the code
end
When new Results are created through Quantifieds, this is what you have in QuantifiedsController#create:
class QuantifiedsController < ApplicationController
def create
#quantified = current_user.quantifieds.build(quantified_params)
if #quantified.save
redirect_to quantifieds_url, notice: 'Quantified was successfully created'
else
#feed_items = []
render 'pages/home'
end
end
end
Well, everything looks perfectly all right, but the problem is actually in line #quantified = current_user.quantifieds.build(quantified_params). All Results created this was have user_id = nil, only the quantified_id is set properly.
How to fix this?
By simplifying the associations! There is no need to store both quantify_id and user_id in Result.
If there is a relation like: User has many Quantifies, and Quantify has many Results, you can quite easily access Results of Users by proper declaration in User:
class User < ActiveRecord::Base
# rest of the code
has_many :quantifieds, dependent: :destroy
has_many :results, through: :quantifieds
# rest of the code
end
This is quite clever thing! Let's see, what happens under the hood:
u = User.last
u.results
Result Load (0.3ms) SELECT "results".* FROM "results" INNER JOIN "quantifieds" ON "results"."quantified_id" = "quantifieds"."id" WHERE "quantifieds"."user_id" = ? ORDER BY date_value DESC [["user_id", 5]]
=> #<ActiveRecord::Associations::CollectionProxy ...>
Now, when relation is set up as described above, the proper counts should appear in website.
There is no need to store user_id in Result, so you can freely remove it!
<div class="stats">
<a href="<%= following_user_path(#jadenmcgruder) %>">
<strong id="following" class="stat">
<%= #jadenmcgruder.quantifieds.count %> #Works
</strong>
Quantified
</a>
<a href="<%= followers_user _path(#jadenmcgruder) %>">
<strong id="followers" class="stat">
<%= #jadenmcgruder.results.good.count %> #Remains zero regardless of number of checked :good boxes
</strong>
Good
</a>
</div>