Im creating an application where an user can see what workout of the day a trainer will tell him to do. This workout is going to be in the homepage of the app.
我正在创建一个应用程序,用户可以看到培训师告诉他要做的当天锻炼。这项训练将在应用程序的主页上进行。
The trainer is going to upload the wods weekly, i want to show just one wod per day.
培训师每周上传一次,我想每天只展示一次。
I thought that if i gave the WOD model an integer attribute called wday, so i can do the logic if Wod.wday == Date.today.cwday i would achieve this. This is not working at all, im getting an error at my static_pages controller.
我认为,如果我给WOD模型一个名为wday的整数属性,那么我可以做逻辑,如果Wod.wday == Date.today.cwday我会实现这一点。这根本不起作用,我的static_pages控制器出错了。
Heres the code:
继承人代码:
Wod model:
Wod型号:
class Wod < ApplicationRecord
belongs_to :user
def this_day
Date.today.cwday == self.wday
end
end
User model:
用户模型:
class User < ApplicationRecord
has_many :wods
def feed
Wod.this_day
end
Static pages controller:
静态页面控制器:
class StaticPagesController < ApplicationController
def home
if logged_in?
@wod = current_user.wods.build
@feed_items = current_user.feed
end
end
def contact
end
def about
end
end
Im getting the following error:
我得到以下错误:
NoMethodError in StaticPagesController#home undefined method `this_day' for #<Class:0x007f5870c1e7b8> Did you mean? third
StaticPagesController中的NoMethodError #home undefined方法`this_day'for#
你的意思是?第三 :0x007f5870c1e7b8>
I would like to know how to get this done, any help would be appreciated!
我想知道如何完成这项工作,任何帮助将不胜感激!
BTW: It works perfectly if i do in the user model Wod.all, but i dont want this
顺便说一句:如果我在用户模型Wod.all中工作,它的工作完美,但我不想要这个
2 个解决方案
#1
1
NoMethodError in StaticPagesController#home undefined method `this_day' for #<Class:0x007f5870c1e7b8>
StaticPagesController中的NoMethodError #home undefined方法`this_day'for#
:0x007f5870c1e7b8>
The problem lies here Wod.this_day
. If you want to call this_day
on WOD
then you need to add it as a class
method.
问题在于Wod.this_day。如果你想在WOD上调用this_day,那么你需要将它添加为类方法。
Change
更改
def this_day
Date.today.cwday == self.wday
end
to
至
def self.this_day
Date.today.cwday == wday
end
Alternate solution is to use scopes
.
替代解决方案是使用范围。
class Wod < ApplicationRecord
belongs_to :user
scope :this_day, -> { where(wday: Date.today.cwday) }
end
#2
0
I fixed it by changing the method this_day
to:
我通过将this_day方法更改为:
def self.this_day
where("wday = ?", Date.today.cwday)
end
#1
1
NoMethodError in StaticPagesController#home undefined method `this_day' for #<Class:0x007f5870c1e7b8>
StaticPagesController中的NoMethodError #home undefined方法`this_day'for#
:0x007f5870c1e7b8>
The problem lies here Wod.this_day
. If you want to call this_day
on WOD
then you need to add it as a class
method.
问题在于Wod.this_day。如果你想在WOD上调用this_day,那么你需要将它添加为类方法。
Change
更改
def this_day
Date.today.cwday == self.wday
end
to
至
def self.this_day
Date.today.cwday == wday
end
Alternate solution is to use scopes
.
替代解决方案是使用范围。
class Wod < ApplicationRecord
belongs_to :user
scope :this_day, -> { where(wday: Date.today.cwday) }
end
#2
0
I fixed it by changing the method this_day
to:
我通过将this_day方法更改为:
def self.this_day
where("wday = ?", Date.today.cwday)
end