Unable to access specific attr_accessor parameters

I use Thinking Sphinx to run the search, and I get the appropriate ActiveRecord models. The problem is that I want to create the appropriate path and link text for each model, and then send the information to the browser as JSON via AJAX. To create these link attributes, I use the following:

In the controller:


class FindController < ApplicationController
  def tag_results
    @results = ThinkingSphinx.search(params[:terms])
    @results.each do |result|
      result.build_ajax_response
    end
    respond_to do |format|
      format.html
      format.json { render :json => @results }
    end
  end
end
In the model:

class TaggedItem < ActiveRecord::Base
  attr_accessible :name
  attr_accessor :search_link, :search_text

def build_ajax_response self.search_link = Rails.application.routes.url_helpers.tagged_item_path (self.id) self.search_text = self.name end end As a result, the json object does not have any of the listed search_ * attributes, and even more so it matters them. I tried using @search_link as well as just search_link in the method build_ajax_response.

Am I doing it wrong? Could there be anything else bothering?

+5
1

Rails to_json , . , :

format.json { render :json => @results.to_json(:methods => [:search_link, :search_text]) }
+8

All Articles