to_json skips attributes with nil value - ruby-on-rails

hi i am new to ruby on rails and using mongoid with rails.
when i try to convert an mongoid object into json the attributes with nil values are skipped.
ruby-1.9.2-p180 :019 >#task
=> #<Task _id: 4e707635c7b4700ce3000004, _type: "Task", created_at: 2011-09-14 09:39:01 UTC, updated_at: 2011-09-14 09:39:01 UTC, due_date: nil, is_completed: false, assignee_id: nil, description: "hi remind this ", user_id: BSON::ObjectId('4e4d1aeac7b4700c6e000096'), item_id: BSON::ObjectId('4e53585fc7b4701082000002')>
#task.to_json(:only=>[:due_date])
=> "{}"
is there any way to get like "{\"due_date\":\"null\"}"

The easiest way to do this is to override the retrieval of due_date.
Try this in your model:
def due_date
real_value = self[:due_date]
return real_value unless real_value.nil?
return "null"
end

Related

Use string as PK and FK on Rails 4

I`m trying to use String as Primary/Foreign key on a small crawler that I'm making. But I keep receiving the following error when I try to use the Associations methods (eg.: a.crawler_details - Where a is an object called Asin):
RangeError: 8532503039 is out of range for ActiveRecord::Type::Integer with limit 4
Record example:
#<Asin asin: "8532503039", title: "O FĂ­sico", image_url: nil, active: false, created_at: "2015-05-04 03:30:29", updated_at: "2015-05-04 03:30:36">
Here are the details:
2.1.2 :001 > Asin.new
=> #<Asin asin: nil, title: nil, image_url: nil, active: true, created_at: nil, updated_at: nil>
2.1.2 :002 > CrawlerDetail.new
=> #<CrawlerDetail id: nil, amazon_price: nil, feed_price: nil, first_place: nil, second_place: nil, third_place: nil, fp_price: nil, sp_price: nil, tp_price: nil, run: nil, created_at: nil, updated_at: nil>
class Asin < ActiveRecord::Base
has_many :crawler_details, :foreign_key => 'id', :primary_key=> 'asin'
self.primary_key = 'asin'
...
end
class CrawlerDetail < ActiveRecord::Base
has_one :asin, :foreign_key => 'asin', :primary_key => 'id'
end
I also tried the belongs_to relation but with no luck. Any ideas here?
I think that is not association error , did you defined field name "asin" type integer in your database table ?
it may help please try to alter your database table from type integer to string .

hstore pql 9.3, rails 4.0.3 storing as symbols?

Really confused here, I thought hstore stores the keys and values as strings, yet I am not getting that in console. It's storing them as a hash, take a look:
2.0.0-p247 :003 > a = Report.new
=> #<Report id: nil, name: nil, column_data: nil, user_id: nil, created_at: nil, updated_at: nil>
2.0.0-p247 :004 > a.column_data = {key: {yes: true}, value: 'hello'}
=> {:key=>{:yes=>true}, :value=>"hello"}
2.0.0-p247 :005 > a
=> #<Report id: nil, name: nil, column_data: {:key=>{:yes=>true}, :value=>"hello"}, user_id: nil, created_at: nil, updated_at: nil>
2.0.0-p247 :006 > a.column_data[:key][:yes]
=> true
2.0.0-p247 :007 > a.column_data[:key]
=> {:yes=>true}
Is something I am doing wrong or is this the new way now?
Thanks!

rails mongodb where in array

i'm trying to get positive_power_values where it's meter_id in array
1.9.3p194 :100 > ppv = PositivePowerValue.first
PositivePowerValue _id: 518dfd25a81fc61ece8d3d23, _type: nil, deleted_at: nil, created_at: "2013-05-11 19:04:54 +0800", updated_at: nil, positive_active_energy_power_indication: "100.00", positive_reactive_power_indication: "50", use_flag: "true", meter_id: "4f71181b6e92fe054b000006"
1.9.3p194 :101 > PositivePowerValue.where(:meter_id.in => ["4f71181b6e92fe054b000006"]).count
=> 0
i wender why is 0?
i need to get positive_power_values where meter_id in array!
Are you using mongoid ?
The proper syntax would be :
PositivePowerValue.in(:meter_id => ["4f71181b6e92fe054b000006"])

.first returning wrong object type

If you look at the four method calls below, Service.first returns a Service object, Salon.first returns a Salon object, etc. But TransactionItem.first returns a Service object. Why could this be?
ruby-1.8.7-p334 :001 > Service.first
=> #<Service id: 147, name: "Fub", salon_id: 2, created_at: "2011-08-10 18:00:07", updated_at: "2011-08-10 18:00:12", price: nil, active: true, archived: true>
ruby-1.8.7-p334 :002 > Salon.first
=> #<Salon id: 1, name: "The Cheeky Strut", created_at: nil, updated_at: nil, address_id: nil, email: nil>
ruby-1.8.7-p334 :003 > Product.first
=> #<Product id: 1, name: "Herbal Essences Shampoo", retail_price: #<BigDecimal:10305f1f0,'0.1E2',9(18)>, wholesale_price: nil, sku: "", salon_id: 2, created_at: "2011-07-08 01:35:48", updated_at: "2011-07-08 01:35:48", archived: false>
ruby-1.8.7-p334 :004 > TransactionItem.first
=> #<Service id: 63, created_at: "2011-08-30 20:05:57", updated_at: "2011-08-30 20:05:57", price: #<BigDecimal:10303eba8,'0.18E2',9(18)>>
ruby-1.8.7-p334 :005 >
This is what my app/models/transaction_item.rb looks like:
class TransactionItem < ActiveRecord::Base
belongs_to :transaction
belongs_to :stylist
end
I blew away the TransactionItem table via a migration, then created a brand new migration to re-create it. That seems to have fixed the problem.

How to populate select box from db query

I am trying to populate a ruby on rails select box from a database query, the data comes from 3 tables.
My query
#data = Session.all :include => { :term => :courses }
Object
!ruby/object:Session
attributes:
created_at: 2010-06-17 22:12:05
term_id: "15"
updated_at: 2010-06-17 22:12:05
id: "3"
course_id: "1"
attributes_cache: {}
term: &id003 !ruby/object:Term
attributes:
number: "1"
start_date: 2010-06-17
created_at: 2010-06-17 22:12:05
updated_at: 2010-06-17 22:12:05
id: "15"
attributes_cache: {}
courses:
- &id001 !ruby/object:Course
attributes:
created_at:
updated_at:
course_name: Beginner
id: "1"
date:
course_type: Programming
attributes_cache: {}
what i am trying to do is to have the term number followed by the data data and then the course
like this
1 01-09-10 Programming Beginners
The id for the option would be the session_id
any ideas ?
Thanks
Alex
in your erb template you can put the following code withing you form:
<%= select("session","id", #data.map{|d| ["#{d.term.number} #{d.term.start_date} #{d.course.course_type} #{d.course.course_name}",d.id]} %>

Resources