I’m using Rails 4.2.3 and MySql 5.5.37. I have a model based on the following db table …
mysql> desc user_objects;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| object | varchar(255) | YES | | NULL | |
| day | date | YES | | NULL | |
| total | int(11) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
In my controller, I’m trying to process parameters submitted from a form and so I use this …
def create
@current_user = User.find(session["user_id"])
…
puts user_object_params
@user_object = UserObject.new(user_object_params)
puts @user_object.object
puts @user_object.day
puts @user_object.total
Even though the first “puts” line prints out “{"object"=>"3", "day"=>"02/28/2016", "total"=>"77"}”, the third puts line (“@user_object.day”) prints out nothing. What do I need to do to get my date object created in my model via the “new” call?
Edit: Here is the definition of user_object in the file "./app/models/user_object.rb".
class UserObject < ActiveRecord::Base
validates :day, :presence => true
validates_numericality_of :total
validates :object, :presence => true
def self.find_total_by_user_object_month_and_year(user_id, object, month, year)
sum(:total, :conditions => ['user_id = ?', user_id, 'object = ?', object, 'month(day) = ?', month, 'year(day) = ?', year])
end
def self.find_total_by_user_object_and_year(user_id, object, year)
sum(:total, :conditions => ['user_id = ?', user_id, 'object = ?', object, 'year(day) = ?', year])
end
end
Edit 2: In response to the answer given, including this in my controller
puts user_object_params[:day]
@user_object.day = user_object_params[:day]
puts @user_object.day
Prints out "02/23/2016" for teh first "puts" but then nothing is printed for the second "puts."