开发者

Combine Scope with current_user

i have a scope like this :

class InventoryItem < ActiveRecord::Base
  belongs_to :inventory
  belongs_to :game_item

  # define custom scopes to get equipped inventory items for user
  scope :equipped, where(:is_equipped => 1)

  scope :item, lambda { |item_ty开发者_如何学Gope|
    joins(:game_item).
    includes(:game_item).
    where("game_items.item_type = ?", item_type ).
    limit(1)
  }

can i get the current_user model that also includes equipped items in one command ? (with includes maybe ?)


Assuming that your inventory belongs to a user, you could include the current_user model using this:

scope :equipped, where(:is_equipped => 1).includes(:inventory => :user)

This tells Rails to include the inventory model and the associated user model for that inventory. If you wanted to do the same for the item scope you could do this:

scope :item, lambda { |item_type|
  includes(:game_item).
  includes(:inventory => :user).
  where("game_items.item_type = ?", item_type ).
  limit(1) }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜