问题描述:

is there a way to load model association without eager loading? im trying to push an object to the browser with model associations included.

push_to_user pushes the @todo object to the browser. a javascript listener will read the data and print it out.

The issue im having is that @todo doesn't include its model association (e.g. "categories"). how can i include that?

@todo = Todo.find(1)

puts @todo.item.categories # eager load categories. works

# push object to pusher

Pusher.push_to_user(@todo, user)

# custom pusher method

def self.push_to_user(todo, user)

Pusher['private-1'].trigger('activity', {:todo => todo, :user => user})

end

# Browser

console.log(todo) # categories are missing

thanks

pete

网友答案:

I think the better is add a new key with this categories :

def self.push_to_user(todo, user)
    Pusher['private-1'].trigger('activity', {:todo => todo, :categories => todo.categories, :user => user})
end
网友答案:

You can call a #serializable_hash method on your todo with an :include => categories

def self.push_to_user(todo, user)
    Pusher['private-1'].trigger('activity', {:todo => todo.serializable_hash(:include => :categories), :user => user})
end

I don't test this solution but can works.

相关阅读:
Top