我想从模型Organiser中通过模型Event获取名称值。
我的班级Organiser
class Organiser < ActiveRecord::Base
belongs_to :customer
has_many :events
end我的Event模型
class Event < ActiveRecord::Base
belongs_to :customer
belongs_to :location
belongs_to :organiser
has_many :subscribers
end在Grails中,这是一个简单的调用:
def eventInstance = Event.find....
def organiserInstance = eventInstance.organiserorganiserInstance.name从组织者那里给了我这个名字。
这是如何在中实现的?我试过:
@orgName = @eventInstance.organiser.name但是我得到了一个错误:
undefined method `organiser' for #<Event::ActiveRecord_Relation:0x007f86152ef098>发布于 2017-05-10 10:55:03
这个错误意味着您的@eventInstance实际上是一个集合,而不是一个实例。
您将首先将@eventInstance设置为Event模型的单个实例。然后,您的代码将工作:
@eventInstance = Event.first
@orgName = @eventInstance.organizer.nameRuby约定使用的是用蛇情况调用变量,而不是用骆驼情况:
@event_instance = Event.first
@org_name = @event_instance.organizer.namehttps://stackoverflow.com/questions/43890400
复制相似问题