Posts mit dem Label named_scope werden angezeigt. Alle Posts anzeigen
Posts mit dem Label named_scope werden angezeigt. Alle Posts anzeigen

Freitag, 7. November 2008

Named scope with variables

in model *.rb (in this case weight_meassurements.rb)

named_scope :user_weight_meassurements, lambda { |user_id| { :order => 'created_at DESC', :conditions => ["user_id == ?", user_id] } }

named_scope :today, lambda { |today| { :conditions => ["created_at > ?", today] } }

Calling from the controllers or helpers @user.weight_meassurements.today('2008-11-07 00:00:00') wil retrieve all weight_meassurement record belonging to the user created after midnight of the passed date.

You can also call WeightMeassurements.user_weight_meassurements(@user.id) for all this user weight meassurement records.

Donnerstag, 26. Juni 2008

named_scope

in the *.rb models

Class Profile << ActiveRecord::Base
belongs_to: user
belongs_to: team
named_scope :captain, :conditions => { :role => "captain" }
named_scope :liverpool, :include :team, :conditions => { 'teams.name' => 'liverpool' }
end

This allows following:

user.profile.liverpool #-> finds all user profiles that are in the team liverpool
user.profile.captain #-> finds all user profiles that have the role of captain.