Ruby on rails 如何为活动记录添加方法?
我想在活动记录上使用getScheduleFixed()方法Ruby on rails 如何为活动记录添加方法?,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,我想在活动记录上使用getScheduleFixed()方法 ps = ProgramSchedule.all ps.getScheduleFixed 重要的事实是如何在方法声明中访问“ps”数组(活动记录) class ProgramSchedule < ActiveRecord::Base def getScheduleFixed array = (HERE ARRAY RETURNED BY ACTIVE RECORD) # some stuff...
ps = ProgramSchedule.all
ps.getScheduleFixed
重要的事实是如何在方法声明中访问“ps”数组(活动记录)
class ProgramSchedule < ActiveRecord::Base
def getScheduleFixed
array = (HERE ARRAY RETURNED BY ACTIVE RECORD)
# some stuff...
return array
end
end
class ProgramSchedule
执行ProgramSchedule.all
时,您会得到一个数组,而不是ProgramSchedule的实例
如果始终使用所有记录调用方法,则可以使用如下类方法:
class ProgramSchedule < ActiveRecord::Base
def self.getAllScheduleFixed
array = ProgramSchedule.all # or self.class.all could be used if you subclass this class
#some stuff
end
end
class ProgramSchedule
如果您只需要处理ProgramSchedule的子集,即条件,则需要将条件传递到此类方法,或直接将结果数组传递给某个类方法。执行ProgramSchedule.all
,您将得到一个数组
,而不是ProgramSchedule
的实例
如果始终使用所有记录调用方法,则可以使用如下类方法:
class ProgramSchedule < ActiveRecord::Base
def self.getAllScheduleFixed
array = ProgramSchedule.all # or self.class.all could be used if you subclass this class
#some stuff
end
end
class ProgramSchedule
如果您只需要处理ProgramSchedule的子集,即条件,则需要将条件传递给此类方法,或直接将结果数组传递给某个类方法。您在这里混为一谈
1)您可以在单个ActiveRecord对象上使用(实例)方法
# Returns an ARRAY with all programschedule instances
all_ps = ProgramSchedule.all
# You can now iterate over over the array
all_ps.each do |ps|
# in here you can call the instance method on the actual model instance
ps.instance_method
end
# Definition of this method in app/models/program_schedule.rb
class ProgramSchedule < ActiveRecord::Base
def instance_method
# Foo
end
end
#返回包含所有programschedule实例的数组
all_ps=程序进度表.all
#现在可以在数组上迭代
所有的P.每个都有|
#在这里,您可以对实际的模型实例调用instance方法
实例法
结束
#在app/models/program_schedule.rb中定义此方法
类ProgramSchedule
2)可以在ActiveRecord模型本身上运行类方法
ProgramSchedule.class_method
# Definition of this method in app/models/program_schedule.rb
class ProgramSchedule < ActiveRecord::Base
def self.class_method
# Bar
end
end
ProgramSchedule.class\u方法
#在app/models/program_schedule.rb中定义此方法
类ProgramSchedule
你把事情搞混了
1)您可以在单个ActiveRecord对象上使用(实例)方法
# Returns an ARRAY with all programschedule instances
all_ps = ProgramSchedule.all
# You can now iterate over over the array
all_ps.each do |ps|
# in here you can call the instance method on the actual model instance
ps.instance_method
end
# Definition of this method in app/models/program_schedule.rb
class ProgramSchedule < ActiveRecord::Base
def instance_method
# Foo
end
end
#返回包含所有programschedule实例的数组
all_ps=程序进度表.all
#现在可以在数组上迭代
所有的P.每个都有|
#在这里,您可以对实际的模型实例调用instance方法
实例法
结束
#在app/models/program_schedule.rb中定义此方法
类ProgramSchedule
2)可以在ActiveRecord模型本身上运行类方法
ProgramSchedule.class_method
# Definition of this method in app/models/program_schedule.rb
class ProgramSchedule < ActiveRecord::Base
def self.class_method
# Bar
end
end
ProgramSchedule.class\u方法
#在app/models/program_schedule.rb中定义此方法
类ProgramSchedule
我认为您应该使用以下范围:
class ProgramSchedule < ActiveRecord::Base
scope :fixed, { all.map(&:getScheduleFixed) }
end
class ProgramSchedule
或
class ProgramSchedule
现在您只需要调用ProgramSchedule.fixed
。这两种方法都可以链接到其他范围,如ProgramSchedule.latest.fixed
。查看更多详细信息我认为您应该使用scope:
class ProgramSchedule < ActiveRecord::Base
scope :fixed, { all.map(&:getScheduleFixed) }
end
class ProgramSchedule
或
class ProgramSchedule
现在您只需要调用ProgramSchedule.fixed
。这两种方法都可以链接到其他范围,如ProgramSchedule.latest.fixed
。查看更多详细信息。。all
返回一个数组,而不是ActiveRecord实例。可以执行ProgramSchedule.getScheduleFixed
?。all
返回一个数组,而不是ActiveRecord实例。您可以执行程序计划。getScheduleFixed
?