Mongoid - how to get second record? - ruby-on-rails

I can do
1.9.3p125 :122 > User.first
=> #<StandardUser _id: 4f849..
and
1.9.3p125 :124 > User.last
and
1.9.3p125 :125 > User.count
=> 5
but I can't find any way to get at the other records (2 thru 4).

User.skip(1).first returns you second document.

Related

Check values in a hash

I am working on a legacy Rails project that uses Ruby 1.8 .
I have an hash of key-value where value is a float number.
For example, my_hash = ['foo'=>12.20, 'bar'=>10.0]. How can I check if my_hash contains all zero values? e.g. ['foo'=>0, 'bar'=>0, 'whatever'=>0] or ['foo'=>0.0, 'bar'=>0, 'whatever'=>0.0].
I know I can loop through & check element one by one, but I just wonder is there a more elegant way?
use below code if my_hash is:
[{"foo"=>0.0, "bar"=>0, "whatever"=>0.0}]
my_hash.first.values.all?{|item| item.zero?}
or if my_hash is array, just:
my_hash.all?{|item| item.zero?}
Check all values in the hash are zero or not?
2.2.3 :006 > h={"foo"=>0.0, "bar"=>0, "whatever"=>0.0}
=> {"foo"=>0.0, "bar"=>0, "whatever"=>0.0}
2.2.3 :007 > h.values.all?{|a| a.zero?}
=> true
If it is an array of hash then
2.2.3 :001 > h = ['foo'=>0.0, 'bar'=>0, 'whatever'=>0.0]
=> [{"foo"=>0.0, "bar"=>0, "whatever"=>0.0}]
2.2.3 :004 > h[0].values.all?{|a| a.zero? }
=> true
OR
2.2.3 :014 > h
=> [{"foo"=>0.0, "bar"=>0, "whatever"=>0.0}]
2.2.3 :015 > h.first.values.all?{|a| a.zero?}
=> true
Any element is zero
2.2.3 :009 > h={"foo"=>0.0, "bar"=>2, "whatever"=>1.1}
=> {"foo"=>0.0, "bar"=>2, "whatever"=>1.1}
2.2.3 :010 > h.values.any?{|a| a.zero?}
=> true
2.2.3 :011 > h={"foo"=>0.2, "bar"=>2, "whatever"=>1.1}
=> {"foo"=>0.2, "bar"=>2, "whatever"=>1.1}
2.2.3 :012 > h.values.any?{|a| a.zero?}
=> false

check if object has been updated mongoid 3.x

1.9.3p448 :014 > l.reload
=> #<Lesson _id: 527246641d41c81d14000006, title: "ola">
1.9.3p448 :015 > l.changed?
=> false
1.9.3p448 :016 > l.changes
=> {}
1.9.3p448 :017 > l.previous_changes
=> {"title"=>["olaaaaa", "ola"]}
1.9.3p448 :018 > l.changed?
=> false
1.9.3p448 :019 > l.update_attributes(title: "olaaa")
=> true
1.9.3p448 :020 > l.changes
=> {}
1.9.3p448 :021 > l.changed?
=> false
I have updated the attribute "title" but when I try l.changed? I get false.
I know the new_record? method, to know if a object is a new object but I need to know if a object is updated.
I would like to know, how can I know if a object has been updated with mongoid 3.x?
When you reload, save, update, the changes are moved to previous_changes. You also have access to the changes on callbacks, otherwise you will have to use previous_changes .
Thats by design, to be consistent with ActiveRecord.

Rails n elements before last

In Rails I often do this:
Model.last(5).first
This retrieves element last-5.
Is there a built-in way of doing this?
The more common way is offset()
Model.offset(5).last
Edit (for lazy people):
1.8.7 :001 > User.first.id
=> 1
1.8.7 :002 > User.last.id
=> 143455
1.8.7 :003 > User.offset(5).last.id
=> 143450

Array length not getting correctly

in view.html.erb page I have code like:
<%
str="D:\\projects\\curator\\java\\hpc"
no=str.count("\\")
splitstr=str.split(pattern="\\",no+1)
%>
After this I printed
<%= splitstr.length %>
It gives me the output as 9. But the splitstr has only 5 elements.
How is it happening.
You end up having four of ""(empty) strings in your array
use this instead..
splitstr=str.split("\\")
splitstr.length
1.9.3p385 :007 > str="D:\\projects\\curator\\java\\hpc"
=> "D:\\projects\\curator\\java\\hpc"
1.9.3p385 :008 > no=str.count("\\")
=> 4
1.9.3p385 :009 > splitstr=str.split(pattern="\\",no+1)
=> ["D:", "projects", "curator", "java", "hpc"]
1.9.3p385 :010 > splitstr.length
=> 5
1.9.3p385 :011 >
Im getting it right!

Why does rails change the type of the attribute _was value after it is set?

I have an attribute in my model that is stored as text but interpreted as a rational. I have this method to handle that:
def start
read_attribute(:start).to_r
end
When I set the start attribute to a new value, the start_was helper method returns a string, instead of a rational, but before I do so, it returns the correct value. Why?
Loading development environment (Rails 3.2.8)
1.9.3p194 :001 > d = Day.find(55)
Day Load (8.7ms) SELECT "days".* FROM "days" WHERE "days"."id" = ? LIMIT 1 [["id", 55]]
=> #<Day id: 55, date: "2012-03-30", start: "1/2", finish: "2/2", created_at: "2012-09-18 15:16:42", updated_at: "2012-09-19 08:20:41", day_year_id: 1>
1.9.3p194 :002 > d.start_was
=> (1/2)
1.9.3p194 :003 > d.start=0
=> 0
1.9.3p194 :004 > d.start_was
=> "1/2"
I think the reason is this method in ActiveModel (activemodel-3.2.8\lib\active_model\dirty.rb)
# Handle <tt>*_was</tt> for +method_missing+.
def attribute_was(attr)
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
end
As you see, if attribute was not actually changed it just calls its own getter, in your case hitting your start method which does the transformation. But if the attribute is actually changed, it reads its raw value from the changed_attributes storage.

Resources