Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Problem with rails 2.1.0 & its...

Replytotopic

Problem with rails 2.1.0 & its older versions

Posted in Forums : Ask a Rails expert

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

hi to everyone .i am using rails 2.1.0 for my current project.in the console mode i tried to excute the statement in console mode

User.find(:first,:select=>”name as text,id as value”).to_json

=> “{\”user\”: {\”text\”: \”thillai\”, \”value\”: \”1\”}}”

in rails 2.1.0 its giving the model name user also

but in rails 2.0.2 the results are coming as

{\”text\”: \”thillai\”, \”value\”: \”1\”}

as per my requirement i want results to be

{\”text\”: \”thillai\”, \”value\”: \”1\”}.

Is there any solution for this problem

 
2230883585_3c07ca3353_s

Authority 25
Posting Rating 49
Sign in to rate this post

Aha! One of the Rail 2.1 gotchas:
Changes in Active Record Attribute Filtering

ActiveRecord::Base#attributes does not allow filtering anymore (it does not accept :only, for example). You must do the filtering manually, with something like this:

1: def json_attributes_for(model, *attrs)
2: attrs = [attrs].flatten.map(&:to_s)
3: model.attributes.delete_if{|k,v| !attrs.include?(k) }.to_json
4: end

Called like so:

json_attributes_for(page, :id, :keyword)

From: http://giantrobots.thoughtbot.com/2008/6/19/gotchas-when-upgrading-to-rails-2-1

So you will have to change the way you call it….

Replytotopic

Other Recent Topics

Ask a Rails expert : copy to clipboard

Ask a Rails expert : url_for

Ask a Rails expert : API call problem

Ask a Rails expert : Invoke a plugin from a plugin?

Ask a Rails expert : Restful problem

Ask a Rails expert : using $F

Ask a Rails expert : Problem with form for ActiveResource model

Ask a Rails expert : Paypal(How to set u/p)

Ask a Rails expert : MySQL for Production?

Ask a Rails expert : Git for Windows

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel