Moosa
Moosa

Reputation: 3216

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. The listing model has a user_id which joins with the user table. The user table has name, profile_image, profile_description.

My routes.tb:

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

My 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. 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>

Upvotes: 0

Views: 114

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Associations

You'll be best looking up about ActiveRecord Associations

ActiveRecord is an ORM (Object Relationship Mapper), which provides a level of abstraction for your application's object associations. 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:

#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:

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). 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.

Since Rails is built on top of Ruby (it's a gem), it's an object orientated framework. 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):

enter image description here

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.

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

Upvotes: 1

Ahmed
Ahmed

Reputation: 819

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>

Upvotes: 1

Related Questions