如何通过提取另一个模型的名称来确定模型?
我有几个模型:ContactEmail、ContactLetter、ContactCall
我的控制器不知道传递给它的内容是什么。它只知道它是一个事件(因此事件可以是 ContactEmail、ContactLetter、ContactCall 的特定实例),
每个事件都有不同的属性。 ContactEmail 有 email_id; ContactLetter 有 letter_id,ContactCall 有 call_id。
所以这是我的问题:
对于任何给定的事件实例,如何访问适当的模型和关联的 ID?
换句话说,如何从事件中提取模型名称(电子邮件、信件),然后分配正确的 id(email_id、letter_id)以找到正确的记录?
我从以下内容开始:
model = event.class # this will show ContactPostalcard
puts event.send('contact_id')
puts model
short_model_name = model.name.sub('Contact','')
puts short_model_name
short_model = short_model_name.class
key_name = short_model_name.foreign_key
puts key_name
asset_id = event.send("#{key_name}")
puts asset_id
asset = short_model_name.send(find.send(asset_id))
我想要做的是找到事件中隐含的关联资产(电子邮件、信件),并获取每个 ContactEmail 中的 email_id(letter_id、voicemail_id)引用的特定资产。
例如: asset = Email.find(ContactEmail.email_id) 会给我资产。所以我需要能够动态创建“电子邮件”,email_id。我在概念上有这些作品,但它不起作用:(。
授予 Chubas,,但想验证这是否是正确的方向(一些附加信息/澄清):
ContactEmail.rb
class ContactEmail
def get_asset
email_id = self.email_id
asset = Email.find_by_email(email_id)
return asset
end
end
这看起来正确吗?
I have several models: ContactEmail, ContactLetter, ContactCall
My controller doesn't know what is being passed into it. It only knows it as an event (so event can be specific instance of ContactEmail, ContactLetter, ContactCall)
Each of these has a different attribute. ContactEmail has email_id; ContactLetter has letter_id, ContactCall has call_id.
So here is my question:
For any given instance of the event, how do I access the appropriate Model and associated ID?
In other words, how can I extract from event the Model Name (Email, Letter), and then assign the right id (email_id, letter_id) to then find the right record?
I started with the following:
model = event.class # this will show ContactPostalcard
puts event.send('contact_id')
puts model
short_model_name = model.name.sub('Contact','')
puts short_model_name
short_model = short_model_name.class
key_name = short_model_name.foreign_key
puts key_name
asset_id = event.send("#{key_name}")
puts asset_id
asset = short_model_name.send(find.send(asset_id))
What I want to do is find the associated asset (Email, Letter) that is implicit in the event, and get the specific asset referenced by the email_id (letter_id, voicemail_id) that is in each ContactEmail.
For example: asset = Email.find(ContactEmail.email_id) would give me the asset. So I would need to be able to dynamically create 'Email', email_id. I have the pieces conceptually, but it doesn't work :(.
Awarded to Chubas, but wanted to validate if this is the right direction (some additional information/clarification):
ContactEmail.rb
class ContactEmail
def get_asset
email_id = self.email_id
asset = Email.find_by_email(email_id)
return asset
end
end
Does this look right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用正则表达式来完成此操作
,但我当然更倾向于在所有这些类中添加方法
contact_field_id
,并且只需创建model_name = event.contact_field_id
即可。不仅更干净,而且更具可扩展性。编辑:
似乎您正在尝试将逻辑放入控制器中以找到从数据库获取的正确实例。这不应该对应于控制器,而应该对应于模型。按照我之前提出的建议,我会这样做:
所以你不必担心找到正确的类,只需调用
@event.contact(id)
即可。也许这就是您正在寻找的You can do it with a regexp
But certainly I'm more partisan of adding a method
contact_field_id
into all these classes, and just makemodel_name = event.contact_field_id
. Not only is cleaner, but more scalable.EDIT:
Seems you are trying to put logic into the controller to find the right instance to get from the database. This shouldn't correspond to the controller, but rather to the model. Following what I've proposed before, I'd do:
So you have not to worry about finding the right class, just call
@event.contact(id)
. Perhaps this is what you're looking for您可以按照 EricBoersma 的建议进行操作,让每个类实现一个公共接口来获取 id。像这样:
然后只要在需要访问正确 ID 的地方使用
event.contact_id
即可。You can do what EricBoersma suggests, and have each class implement a common interface to get the id. Something like this:
Then just use
event.contact_id
wherever you need to access the proper id.我建议让所有这些事件类实现一个具有 getID() 方法的事件接口。每个子类都应该实现该方法以返回其特定的 ID。
但是,我不是 Ruby 专家,所以我不知道这是否可能。
I'd suggest having all of these event classes implement an Event interface which has a getID() method. Each of the subclasses should implement that method to return their particular ID.
However, I'm not a Ruby expert so I don't know if that's possible.