简体   繁体   English

Rails 4-从一个模型调用值到另一个模型

[英]Rails 4 - Calling a value from one model into another

I'm creating a marketplace app where sellers can list items to sell. 我正在创建一个市场应用程序,卖家可以在其中列出要出售的物品。 I am in the process of creating seller pages - a page with seller profile and their specific listings. 我正在创建卖方页面-包含卖方资料及其特定列表的页面。

I've gotten as far as creating the page with seller listings but am having trouble pulling in the name and profile info which is in another model. 我已经尽力创建了带有卖方清单的页面,但是在获取其他模型中的名称和个人资料信息时遇到了麻烦。

For the context here, I have 2 models - a listing model and a user model. 对于这里的上下文,我有2个模型-列表模型和用户模型。 The listing model has a user_id which joins with the user table. 列表模型具有一个user_id,该user_id与user表联接。 The user table has name, profile_image, profile_description. 用户表的名称为profile_image,profile_description。

My routes.tb: 我的routes.tb:

 get '/listings/s/:id' => 'listings#vendor', as: 'vendor'

My listings_controller.rb: 我的listings_controller.rb:

  def vendor
    @listings = Listing.where(user: User.find(params[:id]))
  end

My view: Note that in the first line below I have ???. 我的看法:请注意,在下面的第一行中有???。 I want to pull in user.name in there, which is the sellers name. 我想在其中输入user.name,这是卖家的名字。 How do I pull that in? 我该怎么拉? Once I know that, I can use the same process to pull in other fields from the user model. 知道这一点后,就可以使用相同的过程从用户模型中提取其他字段。

<h4>Listings for ??? </h4>

<div class="center">
  <div class="row">
    <% @listings.each do |listing| %>
    <div class="col-md-3 col-sm-3 col-xs-6">
        <div class="thumbnail" > 
           <%= link_to image_tag(listing.image.url(:medium), class: "img-responsive"), listing, data: { no_turbolink: true } %> 
        </div>
        <div class="caption">
            <h3><%= link_to listing.name.downcase.titleize, listing, data: { no_turbolink: true } %></h3>
            <p><%= number_to_currency(listing.price) %></p>
        </div> 
     </div>
    <% end %>
  </div>
</div>

You can set another instance variable for the user. 您可以为用户设置另一个实例变量。 For example: 例如:

def vendor @user = User.find(params[:id]) @listings = Listing.where(user: @user) end

and then in the view: 然后在视图中:

<h4>Listings for <%= @user.name %> </h4>

Associations 社团协会

You'll be best looking up about ActiveRecord Associations 您最好查找有关ActiveRecord关联的信息

ActiveRecord is an ORM (Object Relationship Mapper) , which provides a level of abstraction for your application's object associations. ActiveRecord是一个ORM(对象关系映射器) ,它为应用程序的对象关联提供了抽象级别。 The importance of this is that if you use it correctly, it will only make Rails run much faster, but also ensure your code is succinct: 这样做的重要性在于,如果正确使用它,它只会使Rails的运行速度大大提高,而且还可以确保您的代码简洁:

#app/models/user.rb
class User < ActiveRecord::Base
   has_many :listings
end

#app/models/listing.rb
class Listing < ActiveRecord::Base
   belongs_to :user
end

This means that if you call a @user object, you'll be able to call the associative data too, like so: 这意味着,如果调用@user对象,则也可以调用关联数据,如下所示:

def vendor
   @user = User.find params[:id]
   @listings = @user.listings
end

The value of this is that the association is then take care of with ActiveRecord (not in your controller). 这样做的价值在于,关联ActiveRecord (不在控制器中)。 If you therefore change your association (change models etc), you'll have a single place to make the required changes, rather than having to pick through all your controllers again 因此,如果您更改关联(更改模型等),则只需一个地方即可进行所需的更改,而不必再次选择所有控制器


Objects 对象

To give you a better understanding about this, you need to consider the role of objects in a Rails application. 为了使您更好地理解这一点,您需要考虑Rails应用程序中objects的角色。

Since Rails is built on top of Ruby (it's a gem ), it's an object orientated framework. 由于Rails是在Ruby(这是gem )之上构建的,因此它是一个面向对象的框架。 Object orientation means more than just being a buzzword - it's an entire practice of programming; 面向对象的意义不只是流行语,它是编程的整个实践; putting the objects for your software at the center of the flow (as opposed to a logic-driven flow): 将软件对象放在流程的中心(与逻辑驱动的流程相反):

在此处输入图片说明

In reality, objects are just variables with a lot of JSON-notation data inside, including things like data-type etc. Rails populates objects in your models, allowing you to then use the objects in a variety of Rails helper methods such as form_for etc. 实际上,对象只是内部包含大量JSON注释数据的变量,包括诸如数据类型之类的东西。Rails填充模型中的对象,从而使您可以在各种Rails帮助器方法(例如form_for等)中使用这些对象。

The reason this is important is because of how object orientation works. 之所以重要,是因为面向对象的工作方式。 You can associate objects, manipulate them & destroy them. 您可以关联对象,对其进行操作并销毁它们。 This means that anything you do in Rails should be based around objects, which is why the association I mentioned above is so important 这意味着,任何你在做的Rails应根据各地的对象,这就是为什么我在上面提到的关系是非常重要的

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

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