简体   繁体   English

如何在 Ruby on Rails 中访问 1 个 controller 中的多个模型

[英]How to access multiple models in 1 controller in Ruby on Rails

A few minutes ago, I saw a user (bruno_pellizzetti) ask a question I had found an answer to while coding my own app with the same stack.几分钟前,我看到一位用户 (bruno_pellizzetti) 在用相同的堆栈编写自己的应用程序时提出了一个我找到答案的问题。

I wanted to share the answer but I could not, as the question was closed right after it was asked 3 hours ago, for supposedly being opinion related and comments are not allowed because my account has no rep (9 yr account lol).我想分享答案,但我不能,因为这个问题在 3 小时前被问到后就被关闭了,因为据说与意见相关并且不允许发表评论,因为我的帐户没有代表(9 年帐户大声笑)。 I tried editing the question so I could re open and answer there but it keeps saying the queue is full...我尝试编辑问题,以便我可以重新打开并在那里回答,但它一直说队列已满......

Thus I post this with a clarified question, because nobody actually answered the original closed one , clearly or directly.因此,我发布了一个明确的问题,因为实际上没有人明确或直接地回答原来的封闭问题。

How does one access multiple models in a different or unrelated controller in Ruby on Rails?如何在 Rails 上的 Ruby 中访问不同或不相关的 controller 中的多个模型?

The original question:原来的问题:

I'm dealing with a question for a little bit and I can't get going... Basically I have two models, one: Client and the second: Work.我正在处理一个问题,但我无法开始......基本上我有两个模型,一个:客户端,第二个:工作。 So I have a Client and a Work basically.所以我基本上有一个Client和一个Work I have a lot of methods that I use inside each of those Model, like... full_name , or work_details for example, all inside the Model of each.我在每个 Model 中使用了很多方法,例如... full_namework_details ,都在每个 Model 中。 But now I am dealing with this problem... I need to combine information of each of the models to create a single word document, you can say... an Invoice, where I need both methods, from Work model and from Client Model.但是现在我正在处理这个问题......我需要结合每个模型的信息来创建一个单词文档,你可以说......一个发票,我需要两种方法,来自 Work model 和来自客户端 Model . So what is the best and correct way to do this?那么最好和正确的方法是什么? Do I manage something inside my existing controller, for example, inside my works_controler or vice versa?我是否在我现有的 controller 中管理某些东西,例如,在我的works_controler 中,反之亦然? I don't want to re write all my Client model methods inside my work controller and I think it would be wrong... Any thoughts?我不想在我的工作 controller 中重新编写我所有的 Client model 方法,我认为这是错误的......有什么想法吗? Best regards I did a diagram, it would look basically like this: https://i.stack.imgur.com/mN9Gb.png最好的问候我做了一个图表,它基本上看起来像这样: https://i.stack.imgur.com/mN9Gb.png

The most organized or conventional "rails way" to achieve this is to make an Invoice Controller.实现此目的的最有组织或传统的“轨道方式”是制作发票 Controller。 This allows you to later add other actions outside of just generating Invoices.这使您可以稍后添加仅生成发票之外的其他操作。

rails g controller Invoice index will generate a controller and a view folder named Invoice, along with an index.html view. rails g controller Invoice index将生成一个 controller 和一个名为 Invoice 的视图文件夹,以及一个 index.html 视图。 (In my app I did a Home Controller and made a feed that accesses many models.) (在我的应用程序中,我做了一个 Home Controller 并制作了一个可以访问许多模型的提要。)

You can thus access the Work model and Client model, with all their built in model methods and attributes by editing the newly created Invoice Controller like so: You can thus access the Work model and Client model, with all their built in model methods and attributes by editing the newly created Invoice Controller like so:

def index
  @invoicework = Work.where(:user_id => current_user.id) #returns all work items that belong_to the currently logged in user and will be nil if not logged in
  @invoiceclient = Client.where(:clientname => "John Doe") #returns all clients who have the client name John Doe and will be nil if no clients have that name
   
  @invoicework.first.work_details #any work model method could be substituted for work_details. first is only necessary if the query returns more than 1 result. otherwise @invoicework.work_details would suffice
  @invoiceclient.first.full_name #any client model method could be used not just full_name, and same deal with the .first method and the query, it's only necessary if there is more than 1 result
end

You can then access this by adding a route that goes to invoice#index .然后,您可以通过添加前往invoice#index的路线来访问它。

I don't know the exact specifics of Bruno app's model relationships, methods, or attributes, however using this approach one can modify those queries to access the appropriate values.我不知道 Bruno 应用程序的 model 关系、方法或属性的确切细节,但是使用这种方法可以修改这些查询以访问适当的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM