How to do a nested includes for n + 1 in Rails - ruby-on-rails

Here's what happens:
I have in my controller:
#products = Spree::Product.all_active
And in the model:
Spree::Product.class_eval do
def self.all_active
includes(:master)
.where('available_on IS NULL OR available_on < ?', Time.now).where(deleted_at: nil)
end
end
And in the view I'm calling something that will look like this:
#products.each do |product|
product.images.each do |image|
image.attachment.url(:product)
end
end
The log is showing something along the lines like this for every single product:
Spree::Image Load (2.6ms) SELECT "spree_assets".* FROM "spree_assets" WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_id" = $1 AND "spree_assets"."viewable_type" = $2 ORDER BY "spree_assets"."position" ASC [["viewable_id", 9], ["viewable_type", "Spree::Variant"]]
Spree::Price Load (0.3ms) SELECT "spree_prices".* FROM "spree_prices" WHERE "spree_prices"."variant_id" = $1 AND "spree_prices"."currency" = 'USD' LIMIT 1 [["variant_id", 9]]
CACHE (0.3ms) SELECT "spree_zone_members".* FROM "spree_zone_members" WHERE "spree_zone_members"."zone_id" = $1 [["zone_id", 2]]
CACHE (0.0ms) SELECT "spree_countries".* FROM "spree_countries" WHERE "spree_countries"."id" IN (204, 49)
CACHE (0.2ms) SELECT "spree_shipping_methods".* FROM "spree_shipping_methods" INNER JOIN "spree_shipping_methods_zones" ON "spree_shipping_methods"."id" = "spree_shipping_methods_zones"."shipping_method_id" WHERE "spree_shipping_methods"."deleted_at" IS NULL AND "spree_shipping_methods_zones"."zone_id" = $1 [["zone_id", 2]]
CACHE (0.0ms) SELECT "spree_calculators".* FROM "spree_calculators" WHERE "spree_calculators"."calculable_type" = 'Spree::ShippingMethod' AND "spree_calculators"."calculable_id" IN (3, 2, 1)
(0.8ms) SELECT COUNT(*) FROM "spree_assets" WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_id" = $1 AND "spree_assets"."viewable_type" = $2 [["viewable_id", 1], ["viewable_type", "Spree::Variant"]]
I also have the bullet gem installed and it is recommending me to do:
N+1 Query detected
Spree::Variant => [:images]
Add to your finder: :include => [:images]
N+1 Query detected
Spree::Variant => [:default_price]
Add to your finder: :include => [:default_price]
I'm not sure where to place this .includes. to find out where Spree::Variant is being called I went to the Rails console:
2.0.0-p481 :001 > Spree::Product.first
Spree::Product Load (1.8ms) SELECT "spree_products".* FROM "spree_products" WHERE "spree_products"."deleted_at" IS NULL ORDER BY "spree_products"."id" ASC LIMIT 1
=> #<Spree::Product id: 1, name: "Ruby on Rails Tote", description: "Debitis facilis impedit natus eos qui vero. Ut qua...", available_on: "2014-07-04 05:44:50", deleted_at: nil, slug: "ruby-on-rails-tote", meta_description: nil, meta_keywords: nil, tax_category_id: 1, shipping_category_id: 1, created_at: "2014-07-04 05:44:51", updated_at: "2014-07-04 05:45:30">
2.0.0-p481 :002 > Spree::Product.first.images
Spree::Product Load (0.8ms) SELECT "spree_products".* FROM "spree_products" WHERE "spree_products"."deleted_at" IS NULL ORDER BY "spree_products"."id" ASC LIMIT 1
Spree::Variant Load (1.0ms) SELECT "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1 AND "spree_variants"."is_master" = 't' LIMIT 1 [["product_id", 1]]
Spree::Image Load (0.8ms) SELECT "spree_assets".* FROM "spree_assets" WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_id" = $1 AND "spree_assets"."viewable_type" = $2 ORDER BY "spree_assets"."position" ASC [["viewable_id", 1], ["viewable_type", "Spree::Variant"]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Spree::Image id: 21, viewable_id: 1, viewable_type: "Spree::Variant", attachment_width: 360, attachment_height: 360, attachment_file_size: 31490, position: 1, attachment_content_type: "image/jpeg", attachment_file_name: "ror_tote.jpeg", type: "Spree::Image", attachment_updated_at: "2014-07-04 05:45:28", alt: nil, created_at: "2014-07-04 05:45:29", updated_at: "2014-07-04 05:45:29">, #<Spree::Image id: 22, viewable_id: 1, viewable_type: "Spree::Variant", attachment_width: 360, attachment_height: 360, attachment_file_size: 28620, position: 2, attachment_content_type: "image/jpeg", attachment_file_name: "ror_tote_back.jpeg", type: "Spree::Image", attachment_updated_at: "2014-07-04 05:45:29", alt: nil, created_at: "2014-07-04 05:45:30", updated_at: "2014-07-04 05:45:30">]>
Where would I add :images and :default_price within this context?

try:
Spree::Product.class_eval do
def self.all_active
includes(master: { products: :images } )
.where('available_on IS NULL OR available_on < ?', Time.now).where(deleted_at: nil)
end
end
includes can be used to fetch multiple models, nested or not.

For those that came here purely for the title:
Post.includes(:user, :group, sub_category: :category)

Related

Method find_by_user_id_and_friend_id does not seem to work - Rails 5

i am unsure why the method
Friendship.find_by_user_id_and_friend_id(user, friend) &
Friendship.find_by_user_id_and_friend_id(friend, user) gives a nil,
could one kindly explain this to me
why does the method Friendship.find_by_user_id_and_friend_id(user,
friend) give a nil and the method
Friendship.find_by_user_id_and_friend_id(user.id, friend.id) does
not? basically could one explain the difference to me
2.3.0 :065 > user
=> #<User id: 1, email: "richill#gmail.com", created_at: "2016-11-22 15:56:19", updated_at: "2016-12-06 11:39:29", firstname: "richill", lastname: "artloe">
2.3.0 :068 > friend
=> #<User id: 2, email: "emma#gmail.com", created_at: "2016-11-22 16:19:25", updated_at: "2016-11-22 16:19:25", firstname: "emma", lastname: "watson">
Friendship.create(user: user, friend: friend, status: 'pending')
Friendship.create(user: friend, friend: user, status: 'requested')
2.3.0 :078 > Friendship.find_by_user_id_and_friend_id(user, friend)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", nil], ["friend_id", nil], ["LIMIT", 1]]
=> nil
2.3.0 :079 >
2.3.0 :079 > Friendship.find_by_user_id_and_friend_id(friend, user)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", nil], ["friend_id", nil], ["LIMIT", 1]]
=> nil
2.3.0 :080 >
i believe i am suppose to get something like the below results:
>> Friendship.find_by_user_id_and_friend_id(user, friend)
=> #<Friendship:0x2bf74ec #attributes={"status"=>"pending", "accepted_at"=>nil,
"id"=>"1", "user_id"=>"1", "position"=>nil, "created_at"=>"2007-01-03 18:34:09",
"friend_id"=>"1198"}>
>> Friendship.find_by_user_id_and_friend_id(friend, user)
=> #<Friendship:0x490a7a0 #attributes={"status"=>"requested", "accepted_at"=>nil
, "id"=>"2", "user_id"=>"1198", "position"=>nil, "created_at"=>"2007-01-03 18:34
:20", "friend_id"=>"1"}>
very strange. it works when placed like this
Friendship.find_by_user_id_and_friend_id(user.id, friend.id)
=> #<Friendship id: 1, user_id: 1, friend_id: 2, status: "pending", created_at: "2016-12-06 11:55:06", updated_at: "2016-12-06 11:55:06">
but does not work when placed like this:
Friendship.find_by_user_id_and_friend_id(user, friend)
=> nil
could one explain why this is to me
what is the difference
2.3.0 :016 > Friendship.find_by_user_id_and_friend_id(user, friend)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", nil], ["friend_id", nil], ["LIMIT", 1]]
=> nil
2.3.0 :017 > Friendship.find_by_user_id_and_friend_id(user.id, friend.id)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", 1], ["friend_id", 2], ["LIMIT", 1]]
=> #<Friendship id: 1, user_id: 1, friend_id: 2, status: "pending", created_at: "2016-12-06 11:55:06", updated_at: "2016-12-06 11:55:06">
2.3.0 :018 >
the find_by_id method is the same as using the where method except it only returns the first record that is found. (See the source code)
Since the method is searching for id's I believe you need to pass the id directly rather than the instance of your object for it to work. This is the same when you use the .find method.

How to sort :order nil?

Basically everytime a page loads this error pops up in the terminal. It doesn't seem to be causing any tangible issues:
NoMethodError (undefined method `each' for nil:NilClass): app/controllers/habits_controller.rb:8:in `sort'
I want to fix it though and I have a general idea how to.
When a new habit is created instead of its :order defaulting to nil it should default to the next highest integer.
HabitsController
def sort
params[:order].each do |id, order|
Habit.where(id: id).update_all(order: order)
end
render nothing: true
end
def habit_params
params.require(:habit).permit(
:user_id,
:trigger,
:tag_list,
:current_level,
:conceal,
:missed_days,
:target,
:reward,
:comment,
:commentable,
:like,
:likeable,
:action,
:order,
:date_started,
:missed_one,
:completed,
:completed_at,
:notes_text,
:notes_date,
:notable,
:note,
:committed => [],
levels_attributes: [
:missed_days,
:days_lost], notes_attributes: [:notable, :note, :notes_text, :notes_date, :_destroy])
end
schema
create_table "habits", force: true do |t|
t.integer "order"
end
rails c
[2] pry(main)> Habit.last
Habit Load (2.9ms) SELECT "habits".* FROM "habits" ORDER BY "habits"."id" DESC LIMIT 1
=> #<Habit:0x007f94e0da39b0
id: 12,
missed_days: 0,
conceal: false,
likes: nil,
date_started: Tue, 15 Sep 2015 00:00:00 EDT -04:00,
trigger: "wake up",
action: "run",
target: "2 miles",
reward: "eat dessert",
user_id: 2,
created_at: Tue, 15 Sep 2015 09:53:31 EDT -04:00,
updated_at: Tue, 15 Sep 2015 09:53:31 EDT -04:00,
order: nil,
completed_at: nil,
committed: ["sun", "mon", "tue", "wed", "thu", "fri", "sat", ""],
strike_date: nil,
missed_days_date: nil>
rails s
Started POST "/habits/sort" for 127.0.0.1 at 2015-09-15 10:02:36 -0400
Processing by HabitsController#sort as */*
User Load (2.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
Habit Load (0.4ms) SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = $1 [["user_id", 2]]
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
CACHE (0.0ms) SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = $1 [["user_id", 2]]
Level Load (0.3ms) SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = $1 ORDER BY "levels"."id" ASC [["habit_id", 11]]
Level Load (0.3ms) SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = $1 ORDER BY "levels"."id" ASC [["habit_id", 12]]
Level Load (0.3ms) SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = $1 ORDER BY "levels"."id" ASC [["habit_id", 10]]
ActsAsTaggableOn::Tag Load (0.3ms) SELECT DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = $1 AND "taggings"."tagger_type" = $2 ORDER BY taggings_count desc LIMIT 20 [["tagger_id", 2], ["tagger_type", "User"]]
(0.3ms) SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = $1 [["user_id", 2]]
Completed 500 Internal Server Error in 51ms
NoMethodError (undefined method `each' for nil:NilClass): app/controllers/habits_controller.rb:8:in `sort'
habit-sort.js
var update_orders, update_remote_orders;
update_remote_orders = function(orders) {
return $.ajax({
url: "/habits/sort",
type: "POST",
data: {
order: orders
},
success: function(data) {}
});
};
update_orders = function() {
var orders;
orders = {};
$("#sortable tr input.order:hidden").each(function(i, o) {
orders[$(o).attr("data-id")] = i;
return $(o).val(i);
});
return update_remote_orders(orders);
};
$(document).ready(function() {
update_orders();
$("#sortable").sortable({
axis: 'y',
handle: ".btn",
stop: function(ui, event) {
return update_orders();
}
});
});

Why does this assignment and each loop return just 1 attribute and not the entire object I expect?

I have a method on my Node model that looks like this:
def users_tagged
#node = self
#tags = #node.user_tag_list
#users = #tags.each do |tag|
User.find_by email: tag
end
end
What I would like to happen is for the #users object to be a collection of User records, but it simply returns a list of email addresses.
This is what this Node object looks like:
> n
=> #<Node id: 6, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 118, media_type: "Video", created_at: "2015-03-09 20:57:19", updated_at: "2015-03-09 20:57:19", circa: nil, is_comment: nil>
> n.user_tags
=> [#<ActsAsTaggableOn::Tag id: 4, name: "gerry#test.com", taggings_count: 1>, #<ActsAsTaggableOn::Tag id: 3, name: "abc#test.com", taggings_count: 1>]
[135] pry(main)> n.user_tag_list
=> ["gerry#test.com", "abc#test.com"]
But when I try to execute that method, this is what I get:
> n.users_tagged
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'user_tags' AND taggings.tagger_id IS NULL) [["taggable_id", 6], ["taggable_type", "Node"]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'gerry#test.com' LIMIT 1
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'abc#test.com' LIMIT 1
=> ["gerry#test.com", "abc#test.com"]
Why does it not return the entire record?
Well, you need to use Array#collect, not the Array#each :
def users_tagged
#node = self
#tags = #node.user_tag_list
#users = #tags.collect do |tag|
User.find_by email: tag
end
end
Read this - what's different between each and collect method in Ruby

Why does validates_presence_of fail on save, even when the condition is not violated

In my Post.rb model, I am doing this:
validates_presence_of :body
In my controller I have this action that I am executing:
def mark_as_published
if #post.unpublished?
#post.published!
redirect_to post_path(#post), notice: "Successfully published."
else
redirect_to post_path(#post), notice: "Post already published"
end
end
This is the error I am getting:
app/controllers/posts_controller.rb:104:in `mark_as_published'
Started PUT "/posts/ebola-death-climbs-past-6-000/mark_as_published" for 67.230.41.168 at 2014-12-10 20:15:29 +0000
app[web.1]: ActiveRecord::RecordInvalid (Validation failed: Body can't be blank):
This is the record in the console:
> Post.last
=> #<Post id: 29, title: "Ebola death climbs past 6,000", photo: nil, body: "Fresh figures from WHO has revealed that the death...", created_at: "2014-12-10 20:08:58", updated_at: "2014-12-10 20:08:58", user_id: 10, ancestry: nil, file: nil, status: 2, slug: "ebola-death-climbs-past-6-000", publication_status: 0, has_eyewitness: false, youtube_embed_code: "", soundcloud_embed_code: "">
Why does this validation fail, only on the save/update, even though the post.body is not blank?
Edit 1
The publication_status is just an enum:
enum publication_status: [ :unpublished, :published ]
Which comes with a set of handy methods, including published?, unpublished?, published!, and unpublished!. The latter two basically toggle the value to be the flag, i.e. unpublished! changes the publication_status to be unpublished and vice versa.
Here are other validations on the Post.rb model:
validates_length_of :body, maximum: 150, too_long: 'The report must be less than 150 words.',
tokenizer: ->(str) { str.scan(/\w+/) }
validates_length_of :title, maximum: 7, too_long: 'The title must be less than 7 words.',
tokenizer: ->(str) { str.scan(/\w+/) }
Edit 2
This is what happens when I mark the record as published in the console:
0> p = Post.last
=> #<Post id: 29, title: "Ebola death climbs past 6,000", photo: nil, body: "Fresh figures from WHO has revealed that the death...", created_at: "2014-12-10 20:08:58", updated_at: "2014-12-10 20:08:58", user_id: 10, ancestry: nil, file: nil, status: 2, slug: "ebola-death-climbs-past-6-000", publication_status: 0, has_eyewitness: false, youtube_embed_code: "", soundcloud_embed_code: "">
irb(main):002:0> p.published!
=> true
irb(main):003:0> p.save
=> true
No SQL is generated. Not sure if this is because I am doing this in production on Heroku.
Edit 3
I tried to publish a record in development, and this is the SQL & server log:
Started PUT "/posts/longword-verylongword-longword-longword-longword-vellylongword-prettylongword/mark_as_published" for 127.0.0.1 at 2014-12-10 19:22:56 -0500
Processing by PostsController#mark_as_published as HTML
Parameters: {"authenticity_token"=>"8kYDxjYS54sGozjSS4ZZwQFJUTtIBgLpEmpAlTRZc4k=", "id"=>"longword-verylongword-longword-longword-longword-vellylongword-prettylongword"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
(2.0ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
(1.2ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'editor') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Completed 422 Unprocessable Entity in 62ms
ActiveRecord::RecordInvalid - Validation failed: Body can't be blank:
When I do it in the console, this is the log:
> p = Post.last
Post Load (0.6ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
=> #<Post id: 37, title: "LongWord VeryLongWord LongWord LongWord LongWord V...", photo: nil, body: "10PP gives you a lot of one on one attention that ...", created_at: "2014-11-27 09:21:06", updated_at: "2014-11-27 09:21:06", user_id: nil, ancestry: nil, file: nil, status: 1, slug: "longword-verylongword-longword-longword-longword-v...", publication_status: 0, has_eyewitness: false, youtube_embed_code: "", soundcloud_embed_code: "">
[21] pry(main)> p.published!
(0.2ms) BEGIN
SQL (2.0ms) UPDATE "posts" SET "publication_status" = $1, "updated_at" = $2 WHERE "posts"."id" = 37 [["publication_status", 1], ["updated_at", "2014-12-11 00:24:18.419390"]]
FriendlyId::Slug Load (1.9ms) SELECT "friendly_id_slugs".* FROM "friendly_id_slugs" WHERE "friendly_id_slugs"."sluggable_id" = $1 AND "friendly_id_slugs"."sluggable_type" = $2 ORDER BY "friendly_id_slugs".id DESC LIMIT 1 [["sluggable_id", 37], ["sluggable_type", "Post"]]
(0.9ms) COMMIT
=> true
From the log I can see clearly you do not retrieve the Post from the database. So you are saving a "new/empty" post. Please verify how your #post variable is set. I am guessing you are using the incorrect before_action now, or your post retrieval is too liberal and defaults to a new post if not found?
One option, if and only if it works within your application's logic, is to limit the validation to creates:
validates_presence_of :body, on: :create
If your app's logic dictates that updates need to run the validation, it would be helpful to see more code in order to better know how #post is populated, what the published/unpublished? methods look like, etc.

Time not updating in heroku

I'm seriously confused what could be going wrong.
I'm logged into heroku run console and trying to update a timestamp on my database.
I've run User.find(6) to see that the user has a :next_click = 2000-01-01... i don't know why it's that value, but anyway, I do User.update 6, {:next_click => Time.utc(2015)} and it seems to update properly saying 2015-01-01 00:00:00, however when I do another User.find(6) it seems the time has switched back because it's not 2015-01-01 00:00:00.
I'm really confused why it's not. Any insight?
SEE ATTACHED SCREENSHOT
irb(main):033:0> User.update 6, {:next_click => Time.utc(2015) }
User Load (34.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
(1.8ms) BEGIN
(2.2ms) UPDATE "users" SET "next_click" = '2015-01-01 00:00:00.000000', "updated_at" = '2012-05-24 00:13:26.197358' WHERE "users"."id" = 6
(2.2ms) COMMIT
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2015-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:13:26">
irb(main):034:0> User.find(6)
User Load (2.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:13:26">
The same thing happens with u = User.find(6) u.next_click = Time.utc(2013) then u.save
irb(main):001:0> u = User.find(6)
User Load (38.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:57:28">
irb(main):002:0> u.next_click = Time.utc(2013)
=> 2013-01-01 00:00:00 UTC
irb(main):003:0> u
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2013-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:57:28">
irb(main):004:0> u.save
(10.9ms) BEGIN
(3.7ms) UPDATE "users" SET "next_click" = '2013-01-01 00:00:00.000000', "updated_at" = '2012-05-24 03:05:46.059530' WHERE "users"."id" = 6
(2.2ms) COMMIT
=> true
irb(main):005:0> u
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2013-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 03:05:46">
irb(main):006:0> User.find(6)
User Load (33.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 03:05:46">
app/models/user.rb:
class User < ActiveRecord::Base
attr_accessible :gold, :name, :next_click, :points
end
You've created a time column. Slightly confusingly, even though in ruby Time stores time + date, in the database world the concept of a pure time of day (ie just hours/minutes/seconds) exists and that's what you get when you add a column of type :time to your migration.
Ruby itself doesn't have a class that represents a time without a date so rails uses an instance of Time but ignores the date bit.
If you want a column that stores both time and date, change the column to a :datetime one (you'll still get a Time instance back in most cases.
In your ActiveRecord can use the data type :timestamp or :datetime to store the correct date format.

Resources