I call this method in my controller:
我在控制器中调用这个方法:
delete_heard_timeline_event(current_user.id, @showable_video.user.id, @showable_video.video.id)
and I definite it in my model:
我在我的模型中确定了它:
def delete_heard_timeline_event(actor_id, subject_id, secondary_subject_id)
TimelineEvent.find_by_actor_id_and_subject_id_and_secondary_subject_id_and_event_type(actor_id, subject_id, secondary_subject_id, 'heard_event').destroy
end
Why is rails telling me that the method is undefined?
为什么rails告诉我这个方法没有定义?
2 个解决方案
#1
1
If you defined this method as instance
method so you need to to call it like this:
如果您将此方法定义为实例方法,那么您需要这样称呼它:
a = YourModelName.new
a.delete_heard_timeline_event(your_parameters)
and if this methods is class
method so call:
如果这个方法是类方法,那么调用:
YourModelName.delete_heard_timeline_event(your_parameters)
#2
0
I see there is already accepted answer, but to answer to your question:
我看到已经有人接受了答案,但要回答你的问题:
if this NoMethodError pops out because of this line:
如果这个NoMethodError因为这条线弹出:
find_by_actor_id_and_subject_id_and_secondary_subject_id_and_event_type
I think Rails can handle max 3 conditions and you have 4 there.
我认为Rails可以处理最多3个条件,这里有4个。
And because you are in Rails 3, you could do it like this:
因为你在Rails 3中,你可以这样做:
class TimelineEvent
def self.delete_heard_event(actor_id, subject_id, sec_id)
self.scoped.where(:actor_id => actor_id,
:subject_id => subject_id,
:secondary_subject_id => sec_id,
:event_type => 'heard_event').first.try(:destroy)
end
end
The .try method at the end tries to fire a method in it's parameter, but only on non-nil objects, so you are safe to call it with wrong parameters.
最后的.try方法尝试在它的参数中触发一个方法,但是只在非nil对象上使用,所以您可以安全地调用错误的参数。
Then just:
然后:
TimelineEvent.delete_heard_event(current_user.id, @showable_video.user.id, @showable_video.video.id)
Regards, NoICE
问候,吵
#1
1
If you defined this method as instance
method so you need to to call it like this:
如果您将此方法定义为实例方法,那么您需要这样称呼它:
a = YourModelName.new
a.delete_heard_timeline_event(your_parameters)
and if this methods is class
method so call:
如果这个方法是类方法,那么调用:
YourModelName.delete_heard_timeline_event(your_parameters)
#2
0
I see there is already accepted answer, but to answer to your question:
我看到已经有人接受了答案,但要回答你的问题:
if this NoMethodError pops out because of this line:
如果这个NoMethodError因为这条线弹出:
find_by_actor_id_and_subject_id_and_secondary_subject_id_and_event_type
I think Rails can handle max 3 conditions and you have 4 there.
我认为Rails可以处理最多3个条件,这里有4个。
And because you are in Rails 3, you could do it like this:
因为你在Rails 3中,你可以这样做:
class TimelineEvent
def self.delete_heard_event(actor_id, subject_id, sec_id)
self.scoped.where(:actor_id => actor_id,
:subject_id => subject_id,
:secondary_subject_id => sec_id,
:event_type => 'heard_event').first.try(:destroy)
end
end
The .try method at the end tries to fire a method in it's parameter, but only on non-nil objects, so you are safe to call it with wrong parameters.
最后的.try方法尝试在它的参数中触发一个方法,但是只在非nil对象上使用,所以您可以安全地调用错误的参数。
Then just:
然后:
TimelineEvent.delete_heard_event(current_user.id, @showable_video.user.id, @showable_video.video.id)
Regards, NoICE
问候,吵