English 中文(简体)
铁路:我如何利用数据库进行航空管理?
原标题:Rails: How do I unserialize from database?

我目前正在设法为发票/账单提取信息。 在发票上,我要表明总价是多少。 程序和营地;物品、价格和价格。 因此,我希望最终能够看到:

   Consult [date] [total_price]
      Procedure_name [price] [qty]
      Procedure_name [price] [qty]
   Consult [date] [total_price]
      Procedure_name [price] [qty]
   etc...

所有这些信息均可通过数据库获取,但希望将信息作为单独的副本予以节省。 如果用户改变某些程序的价格,发票信息仍然正确。 我认为,通过在发票表中将数据编成一个栏(协商数据)并加以节省,这样做。


我的模式:

class Invoice < ActiveRecord::Base
   ...stuff...
   serialize :consult_data
   ...
end

这是我从形式上获得的(1项咨询和3项程序):

{"commit"=>"Save draft", "authenticity_token"=>"MZ1OiOCtj/BOu73eVVkolZBWoN8Fy1skHqKgih7Sbzw=", "id"=>"113", "consults"=>[{"consult_date"=>"2010-02-20", "consult_problem"=>"ABC", "procedures"=>[{"name"=>"asdasdasd", "price"=>"2.0", "qty"=>"1"}, {"name"=>"AAAnd another one", "price"=>"45.0", "qty"=>"4"}, {"name"=>"asdasdasd", "price"=>"2.0", "qty"=>"1"}], "consult_id"=>"1"}]}

我的拯救行动:

  def add_to_invoice
   @invoice = @current_practice.invoices.find_by_id(params[:id])
   @invoice.consult_data=params[:consults]
   if @invoice.save
    render :text => "I think it worked"
   else
    render :text => "I don t think it worked "
   end
  end

数据库除外,如果我看一看奥利斯群岛的条目,我可以发现那里有:

consult_data: "--- 
- !map:HashWithIndifferentAccess 
  consult_da..."

(---The question---) But I can t seam to get back my data. I tried defining a variable to the consult_data attribute and then doing "variable.consult_problem" or "variable[:consult_problem]" (also tried looping) but it only throws no-method-errors back at me.

我如何利用数据库的数据,将其重新定位为能够使用的信息库?


非常感谢你们的任何帮助!

最佳回答

我认为,你应当确定目标类别。

IE:

serialize :consult_data, Hash

当时

invoice = Invoice.last
invoice.consult_data[:date] # will return date

等等

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签