Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to pass outside variables into the serializer? #1278

Closed
dmastylo opened this issue Oct 20, 2015 · 6 comments
Closed

Is it possible to pass outside variables into the serializer? #1278

dmastylo opened this issue Oct 20, 2015 · 6 comments

Comments

@dmastylo
Copy link

I have a collection that, in order to save db trips, uses a second collection that has some data associated to the records in the first collection. In RABL I could simply reference the global variable @data inside the view template and be done. Is this doable with AMS?

@beauby
Copy link
Contributor

beauby commented Oct 20, 2015

Could you be more specific?

@dmastylo
Copy link
Author

Removing some code for brevity but this is the gist of it. I can't do the method call within the serializer because I'd be doing multiple DB hits for it, so I collect all the results and call the hash within the serializer.

Controller

def index
    @games = Game.games

    # returns a hash
    @temp_players = GameQueue.players(params[:input])

    render json: @games, include: [:perimeters, :start_zones]
  end

Serializer

class GameSerializer < ActiveModel::Serializer
  attributes :id, :name, :max_players, :description, :level, :best_finish

  attribute :buy_in_price, key: :buy_in_price_dollars

  has_many :perimeters, serializer: PerimeterSerializer
  has_many :start_zones, serializer: StartZoneSerializer

  def best_finish
    if @players
      @players[object.id]
    end
  end
end

@bf4
Copy link
Member

bf4 commented Oct 20, 2015

what's the variable you're trying to pass in? Check out
#1252

In the past, I've created an attr_accessor on my model and used that to
pass in info

On Mon, Oct 19, 2015 at 8:09 PM, Damian Mastylo [email protected]
wrote:

Controller

def index
@games = Game.games

# returns a hash
@temp_players = GameQueue.players(params[:input])

render json: @games, include: [:perimeters, :start_zones]

end

Serializer

class GameSerializer < ActiveModel::Serializer
attributes :id, :name, :max_players, :description, :level, :best_finish

attribute :buy_in_price, key: :buy_in_price_dollars

has_many :perimeters, serializer: PerimeterSerializer
has_many :start_zones, serializer: StartZoneSerializer

def best_finish
if @mongo_players
@mongo_players[object.id]
end
endend


Reply to this email directly or view it on GitHub
#1278 (comment)
.

@dmastylo
Copy link
Author

@bf4 @beauby I want to pass in that @players variable from the controller.

@beauby
Copy link
Contributor

beauby commented Oct 20, 2015

@dmastylo You could add attr_accessor :best_finish to your Game model, then in the controller you could do @games.each { |g| g.best_finish = @temp_players[g.id] }.

@dmastylo
Copy link
Author

Thanks that works! @beauby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants