Rails POST Form Don't Send Full of Data to Database - ruby-on-rails

I work on beginner level web application which is save off days request of users by using simple form.
Here is codes
views/days/index.html.erb
<%= form_for #daysoff, url: {action: "create"} do |f| %>
<%= f.date_field :offdate, id: 'altField' %>
<p> User holding the fort / Yerine bakicak kisi
<%= f.collection_select(:assign_id, #people, :id, :name) %> </p>
<%= f.hidden_field :user_id, value: #user.id %>
<%= f.submit "Submit" %>
<% end %>
controllers/days_controller.rb
class DaysController < ApplicationController
unloadable
def index
#people = User.all
#user = User.current
#daysoff = Daysoff.new
# params[:daysoff][:user_id] = #user.id
end
def create
off_dates = params["daysoff"]["offdate"].try(:split,',')
off_dates.each do |off_date|
#days_off = Daysoff.new
#days_off.offdate = Date.strptime(off_date.strip, "%m/%d/%Y")
#days_off.user_id = User.current
#days_off.assign_id = params["assign_id"]
#days_off.status = 0
#days_off.save!
end
redirect_to "/days_off_redmine", :flash => { :success => "Istek gonderildi." }
end
There is no any model validation.
Database migration file.
class CreateDaysoffs < ActiveRecord::Migration
def change
create_table :daysoffs do |t|
t.date :offdate
t.integer :user_id
t.integer :assign_id
t.boolean :status
end
end
end
When i try to fill and post form,
Offdates and status filling correctly but user_id and assign_id insert db as NULL.
+----+------------+---------+-----------+--------+
| id | offdate | user_id | assign_id | status |
+----+------------+---------+-----------+--------+
| 1 | 2015-07-14 | NULL | NULL | 0 |
| 2 | 2015-07-15 | NULL | NULL | 0 |
| 3 | 2015-07-14 | NULL | NULL | 0 |
| 4 | 2015-07-15 | NULL | NULL | 0 |
| 5 | 2015-07-22 | NULL | NULL | 0 |
| 6 | 2015-07-23 | NULL | NULL | 0 |
| 7 | 2015-07-21 | NULL | NULL | 0 |
| 8 | 2015-07-22 | NULL | NULL | 0 |
| 9 | 2015-07-21 | NULL | NULL | 0 |
| 10 | 2015-07-22 | NULL | NULL | 0 |
| 11 | 2015-07-21 | NULL | NULL | 0 |
| 12 | 2015-07-22 | NULL | NULL | 0 |
| 13 | 2015-07-29 | NULL | NULL | 0 |
| 14 | 2015-07-30 | NULL | NULL | 0 |
| 15 | 2015-07-29 | NULL | NULL | 0 |
| 16 | 2015-07-30 | NULL | NULL | 0 |
+----+------------+---------+-----------+--------+
Why this occur like that?

Skipping the fact that code looks really bad...
form attributes are present in params[:daysoff], you would need to get to fields by it, for example:
to get assign_id by params[:daysoff][:assign_id]
#days_off.user_id = User.current should be rather: #days_off.user_id = User.current.id
or #days_off should have in model class added
belongs_to :user and it would be working fine, unless User.current is nil

Related

Active Record querying with joins and group by

I'm designing an API to get data from the following scenario :
brands table :
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
items table :
+---------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| category_id | bigint(20) | YES | MUL | NULL | |
| brand_id | bigint(20) | YES | | NULL | |
+---------------------------+--------------+------+-----+---------+----------------+
item_skus table :
+---------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| item_id | bigint(20) | YES | MUL | NULL | |
| number_of_stock | int(11) | YES | | NULL | |
+---------------------------+--------------+------+-----+---------+----------------+
Item model association with ItemSku and Brand
belongs_to :brand
has_many :skus, class_name: 'ItemSku'
Simply i want the counts of stock available items and all items for each brand.
{
"brandCounts":[
{
"id":7006,
"name":"Brand 01",
"stockAvailableItemCount":50,
"allItemCount":60
},
{
"id":20197,
"name":"Brand 02"
"availableItemCount":150,
"allItemCount":660
}
]
}
Implementation :
brand_counts = []
brand_counts_hash = Hash.new()
items = Item.left_outer_joins(:skus).where(category_id: params[:id]).pluck(:brand_id, :number_of_stock, :item_id)
items.each do |item|
brand_id = item[0]
stock = item[1]
if brand_counts_hash.has_key?(brand_id)
item_count_arry = brand_counts_hash[brand_id]
stock_available_item_count = item_count_arry[0]
all_item_count = item_count_arry[1]
if stock > 0
brand_counts_hash[brand_id] = [stock_available_item_count + 1, all_item_count + 1]
else
brand_counts_hash[brand_id] = [stock_available_item_count, all_item_count + 1]
end
else
stock_available_item_count = 0
all_item_count = 0
if stock > 0
stock_available_item_count += 1
all_item_count += 1
brand_counts_hash[brand_id] = [stock_available_item_count, all_item_count]
else
all_item_count += 1
brand_counts_hash[brand_id] = [stock_available_item_count, all_item_count]
end
end
end
brand_counts_hash.each do |key, value|
stock_available_item_count = value[0]
all_item_count = value[1]
brand_counts << {
id: key,
name: get_brand_name(key),
stock_available_item_count: stock_available_item_count,
all_item_count: all_item_count
}
end
#brand_counts = brand_counts
render 'brands/counts/index', formats: :json
end
def get_brand_name(brand_id)
brand = Brand.find_by(id: brand_id)
brand.name unless brand == nil
end
Is there a way to optimize this further without multiple loops maybe?
Assume your Brand model also has the following association defined
has_many :items
and the final result you want is like
{
"brandCounts":[
{
"id":7006,
"name":"Brand 01",
"stockAvailableItemCount":50,
"allItemCount":60
},
{
"id":20197,
"name":"Brand 02"
"availableItemCount":150,
"allItemCount":660
}
]
}
The following code may not work when you copy and paste to your project. But it demonstrate how this problem can be solved with less code
Brand.includes(items: :skus).all.map do |brand|
{
id: brand.id,
name: brand.name,
stockAvailableItemCount: brand.items.count,
allItemCount: brand.items.map {|item| item.skus.sum(:number_of_stock)}.sum
}
end
if you need json format, just use to_json to the result of above code.

rails uninitialized constant controller param

I added an attribute to a model :roleid I have ran migrations and confirmed it is in the database. The error is happening in the controller.
Only, when I input a value into the form, it throws the error: uninitialized constant User::Roleid
It highlights the line #user = User.new(user_params) from the create action:
user_params is defined as the following:
params.require(:user).permit(:id, :email, :encrypted_password, :fname, :lname, :roleid)
As you can see, roleid exists:
mysql> DESCRIBE users;
+------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | UNI | | |
| encrypted_password | varchar(255) | NO | | | |
| reset_password_token | varchar(255) | YES | UNI | NULL | |
| reset_password_sent_at | datetime | YES | | NULL | |
| remember_created_at | datetime | YES | | NULL | |
| sign_in_count | int(11) | NO | | 0 | |
| current_sign_in_at | datetime | YES | | NULL | |
| last_sign_in_at | datetime | YES | | NULL | |
| current_sign_in_ip | varchar(255) | YES | | NULL | |
| last_sign_in_ip | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| fname | varchar(255) | YES | | NULL | |
| lname | varchar(255) | YES | | NULL | |
| roleid | int(11) | YES | | NULL | |
+------------------------+--------------+------+-----+---------+----------------+
The full error text is:
Started POST "/users" for ::1 at 2016-03-17 17:23:31 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"48xz8D0jngCGOnL7qn4A93X2qeOcmvcE8QjXah7cjsxusA7FrPK8JhmVjRNa2nflkg4qfLs5cnL2oL9cHn/iUQ==", "user"=>{"id"=>"4", "email"=>"wlrg", "encrypted_password"=>"[FILTERED]", "fname"=>"flh", "lname"=>"wflh", "roleid"=>"4"}, "commit"=>"Create User"}
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms)
NameError (uninitialized constant User::Roleid):
app/controllers/users_controller.rb:27:in `create'
The controller code looks like:
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end

Why does this before_save not update the verified date in rails?

My before_save doesn't update the verified_date field.
Why is that? Other processes can update the field ok.
Model:
class Link < ActiveRecord::Base
belongs_to :group
validates_presence_of :url_address
validates_presence_of :group_id
validates_size_of :version_number, :maximum => 10 #, :allow_nil => true
before_save :verify_this_link
acts_as_list
...
def verify_this_link
verified_date = Time.now
end
end
mysql> describe links;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| url_address | varchar(255) | NO | | NULL | |
| alt_text | varchar(255) | YES | | NULL | |
| group_id | int(11) | YES | | NULL | |
| position | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| version_number | varchar(255) | YES | | NULL | |
| content_date | date | YES | | NULL | |
| verified_date | date | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
mysql> select id, substr(url_address,1,20),
verified_date from links where id > 350;
+-----+--------------------------+---------------+
| id | substr(url_address,1,20) | verified_date |
+-----+--------------------------+---------------+
| 351 | http://magicmodels.r | NULL |
| 352 | http://jsbin.com/#ja | 2014-07-12 |
| 353 | http://www.javascrip | 2014-07-12 |
| 354 | http://www.test.com | 2014-08-08 |
| 357 | http://www.t5.com | 2014-07-12 |
+-----+--------------------------+---------------+
5 rows in set (0.00 sec)
Try:
def verify_this_link
self.verified_date = Time.now
end
Reference https://stackoverflow.com/a/6326323/252671

filtering a filtered a Cypher query result

Hi here's my current query i'd like to 're-filter':
START movie = node(*)
MATCH user-[:LIKE]->category-[:SIMILAR*0..3]-()<-[:TAGGED]->movie
WHERE user.name = "current_user"
WITH DISTINCT movie, user, category
RETURN user.name, category.name, ID(movie), movie.name
ORDER BY movie.name;
http://console.neo4j.org/r/u19iim
Here's how it looks like after current query:
+--------------+----------------+-----------+-------------------------+
| user.name | category.name | ID(movie) | movie.name |
+--------------+----------------+-----------+-------------------------+
| current_user | c | 14 | movie_c_and_d_and_e |
| current_user | d | 14 | movie_c_and_d_and_e |
| current_user | e | 14 | movie_c_and_d_and_e |
| current_user | a | 9 | movie_of_a_and_b_and_b1 |
| current_user | b | 9 | movie_of_a_and_b_and_b1 |
| current_user | b | 10 | movie_of_b2_first |
| current_user | b | 11 | movie_of_b2_second |
| current_user | c | 12 | movie_of_c |
| current_user | d | 13 | movie_of_d_and_e |
| current_user | e | 13 | movie_of_d_and_e |
+--------------+----------------+-----------+-------------------------+
I'd like to GROUP BY COUNT(sugg) AS category_count to extract this:
+--------------+----------------+-----------+-------------------------+
| user.name | category_count | ID(movie) | movie.name |
+--------------+----------------+-----------+-------------------------+
| current_user | 3 | 14 | movie_c_and_d_and_e |
| current_user | 2 | 9 | movie_of_a_and_b_and_b1 |
| current_user | 2 | 13 | movie_of_d_and_e |
| current_user | 1 | 10 | movie_of_b2_first |
| current_user | 1 | 11 | movie_of_b2_second |
| current_user | 1 | 12 | movie_of_c |
+--------------+----------------+-----------+-------------------------+
How can I accomplish this?
Similar questions:
- how to have two aggregation in cypher query in neo4j?
Update
Here's the working result (with demo: http://tinyurl.com/cywlycc):
START movie = node(*)
MATCH user-[:LIKE]->category-[:SIMILAR*0..3]-()<-[:TAGGED]->movie
WHERE user.name = "current_user"
WITH DISTINCT movie, category WITH COUNT(movie) AS category_count, movie, collect(category.name) as categorized
RETURN category_count, ID(movie), movie.name, categorized
ORDER BY category_count DESC;
START movie = node(*)
MATCH user-[:LIKE]->category-[:SIMILAR*0..3]-()<-[:TAGGED]->movie
WHERE user.name = "current_user"
WITH DISTINCT movie, user, category
RETURN user.name, count(category.name) as category_count, ID(movie), movie.name
ORDER BY category_count desc, movie.name asc
http://console.neo4j.org/r/69rfkn

Rails ActiveRecord group_by & sum db results for use with Lazy HighCharts

I am completely new to RoR/Ruby and i am using Lazy High Charts gem to generate some purdy charts based on some database information.
I have tried the answers that were provided in a previous question but i am still a bit confused as to how to do this..
I need to sum amount_used, and billed_amount and group by month/year (e.g; Aug/2012)
The end result will be something similar to a dual axis chart with two series "Amount Used", and "Cost".. This information is specific to a certain account_id.
Invoices table
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_id | int(11) | YES | | NULL | |
| invoice_date | varchar(255) | YES | | NULL | |
| amount_used | float | YES | | NULL | |
| billed_amount | float | YES | | NULL | |
| comments | text | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
Controller Chart Code
#account = Account.find(params[:id])
#invoices = Invoice.where("account_id = #{#account.id}").order("invoice_date DESC")
#h = LazyHighCharts::HighChart.new('area') do |f|
f.options[:chart][:defaultSeriesType] = "area"
#Sample dates right now, should be the grouped_by :invoice_date
f.xAxis( :categories => ['May', 'Jun', 'Jul'] )
f.yAxis([
{
:title => { :text => "Amount Used" }
},
{
:title => { :text => "Cost" },
:opposite => true
}
])
#Sample data right now, should be the summed amounts of the :amount_used correpsonding for each above grouped invoice_date
f.series(:name => "Amount Used", :data => [100,300,500] )
#Sample data right now, should be the summed amounts of the :billed_amount correpsonding for each above grouped invoice date
f.series(:name => "Cost", :yAxis => 1, :data => [200,400,600] )
end
It looks like you have everything in place. Here's how you can pull data from db:
#aggregated_invoices = Invoice.
where(:account_id => params[:id]).
order("invoice_date DESC").
group("invoice_date").
select("DATE_FORMAT(invoice_date, '%Y-%m-01') AS invoice_date, sum(amount_used) AS amount_used, sum(billed_amount) AS billed_amount")
# Then use these instead of sample data:
#categories = #aggregated_invoices.map {|i| i.invoice_date.strftime("%b/%Y")}
#amount_used_data = #aggregated_invoices.map(&:amount_used)
#billed_amount_data = #aggregated_invoices.map(&:billed_amount)

Resources