Rails: Better understanding dynamic scopes -
this "dynamic" scope:
def all_games(conditions = {}) scope = games.includes(:stats).scoped {} scope = scope.where sport_position_id: conditions[:sport_position_id] unless conditions[:sport_position_id].nil? scope = scope.where sport_id: conditions[:sport_id] unless conditions[:sport_id].nil? scope = scope.where team: conditions[:team] scope.order(:date) end
the method above in module included user
model.
so access in code, so:
u = user.find(1) u.all_games(sport_position_id: params[:sport_position_id], sport_id: current_sport.id, team: params[:team])
when doing google searching dynamic scopes, came across ryan bate's railscast on anonymous scopes
(http://railscasts.com/episodes/112-anonymous-scopes). modified since using rails 3, wondering, if on right path when comes writing dynamic scopes?
i find myself writing dynamic scopes because of nature of complex api's writing.
Comments
Post a Comment