0

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."

Dave
  • 15,639
  • 133
  • 442
  • 830
  • do `@user_object.object` and `@user_object.total` have the correct values? – jvillian Feb 29 '16 at 20:47
  • What is the class definition for UserObject and what is its responsibility? Is that some sort of form object, or what is it? – Josh Deeden Feb 29 '16 at 20:48
  • If you successfully save a date in that column then everything you've done here should work. – tadman Feb 29 '16 at 20:52
  • I included the class definition for UserObject. It is meant to mirror the database table I included. And no, I cannot save teh date column successfully -- it saves as null. – Dave Feb 29 '16 at 21:34
  • If you can save the other fields (not sure you can - that's why I asked), I wonder if it's a finicky Ruby date formatting issue. – jvillian Feb 29 '16 at 22:05
  • Hi, I suspect its a date formatting issue as well. How do I tell Rails to accept things of a certain date format? Rails seems pretty smart about stuff like this and that this kind of thing can be done with minimal coding. – Dave Feb 29 '16 at 22:13

2 Answers2

0

it looks like you are getting the params correct, However to check if the issue is in the UserObject.new(user_object_params) part, what you can try is try to individually assign values

@user_object = UserObject.new
@user_object.object = user_object_params[:object]
@user_object.day = user_object_params[:day]
@user_object.total = user_object_params[:total]

and then try to print the values.

I have a feeling that the attribute object is conflicting with some rails internal attributes.

to test this, you can individually assign value except object attr. or remove the object key in the hash.

sameera207
  • 16,547
  • 19
  • 87
  • 152
  • Tried your suggestion but still no dice. THis line "@user_object.day = user_object_params[:day]" does not save the value of what is submitted. This has something to do with the fact that my model's field is a date, right? How do I tell Rails to accept the submitted date with a certain format? – Dave Feb 29 '16 at 22:12
  • how about manually change the format, something like `user_object_params[:day].gsub('/','-')` – sameera207 Feb 29 '16 at 22:52
  • Sure, I coudl give that a shot, but is that the Rails way? It seems like tehre would be some setting somewhere that I coudl tell Rails waht my date format shoudl be. Does such a setting exist? – Dave Mar 01 '16 at 03:12
0

Going to open an answer, but expect it will require some iteration.

Assuming @user_object.object and @user_object.total are behaving correctly (you need to confirm this), I suspect you're experiencing finicky Ruby Date (or related) problems.

Make sure that (a) @user_object.day is defined (in your migration) in the format that you want (are you storing a String, Date, or DateTime?) and that (b) user_object_params[:day] is formatted in a way that matches the type defined in your migration.

There are well-know subleties (read, differences) is how Ruby and Rails handle Dates and DateTimes. Maybe this is the root of your problem.

To convert a date string into a DateTime, you're going to have to do something like:

DateTime.strptime("06/18/2013", "%m/%d/%Y")

as discussed here. So, possibly before doing @user_object.new(user_object_params), you might need to do something like:

user_object_params[:day] = DateTime.strptime(user_object_params[:day], "%m/%d/%Y")

Fishing here, but thought it might help.

Community
  • 1
  • 1
jvillian
  • 19,953
  • 5
  • 31
  • 44
  • The column is dfeined in my migration like so: "t.date :day". Regarding this, "user_object_params[:day] is formatted in a way that matches the type defined in your migration," I don't understand what you mean by this. Can you give an example, preferably one taht uses the format, "%m/%d/%Y"? – Dave Mar 01 '16 at 03:18
  • Hi, Dave. I did give the example. See the strptime line above. Did you give it a try? – jvillian Mar 01 '16 at 03:32