Anthony
Anthony

Reputation: 67

Rails Activerecord not returning values

Rails question...

I am trying to print out data from a few tables that are joined together in my db in a view. Specifically, I want to print out each property address ("address" from Property Model) and it's corresponding comment ("comment_text" from Interest Model)

Questions:

1) Right now I am joining the tables in the View; however, I assume this is not correct, but am unsure of how to join them in the controller. Should I be joining the tables inside the view at all? Or should this be happening in the controller?

2) In my View, I want to print out the address and the corresponding comment_text; however, when I try to print out right now, it looks like an active record relation object, i.e. shows "["test"]" and I want it to show "test"

Any ideas? Code below:


Schema:

create_table "interests", force: :cascade do |t|
  t.integer  "user_id"
  t.integer  "property_id"
  t.string   "interested",   default: "unknown"
  t.string   "comment_text"
  t.datetime "created_at",                       null: false
  t.datetime "updated_at",                       null: false
end

add_index "interests", ["property_id"], name: "index_interests_on_property_id"
add_index "interests", ["user_id"], name: "index_interests_on_user_id"

create_table "properties", force: :cascade do |t|
  t.string   "address"
  t.string   "city"
  t.string   "state"
  t.integer  "zip"
  t.datetime "created_at",                             null: false
  t.datetime "updated_at",                             null: false
end

Models:

class Property < ActiveRecord::Base
  has_many :interests
  has_many :users, through: :interests

  default_scope {order("id ASC")}
end

class Interest < ActiveRecord::Base
  belongs_to :property
  belongs_to :user

  validates :user_id, :presence => true
  validates :property_id, :presence => true
end

Controller:

def index
  @properties = Property.all
end

View:

<% @properties.each do |p| %>
    <td><%= p.interests.collect(&:comment_text) %>
<% end %>

Upvotes: 1

Views: 359

Answers (2)

Rahu1
Rahu1

Reputation: 41

I think it would be great if you use include.With includes, Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries.

ex- Controller:

def index
 @properties = Property.includes(:interests)
end

view:

<% @properties.each do |property| %>
  property.interests.each do |interest|
    <td><%=interest.comment_text %>
 <% end %>
<% end %>

Ref ActiveRecord QueryMethods

Upvotes: 0

slowjack2k
slowjack2k

Reputation: 2586

1) Right now I am joining the tables in the View; however, I assume this is not correct, but am unsure of how to join them in the controller. Should I be joining the tables inside the view at all? Or should this be happening in the controller?

The answer is opinion based. Some people favour DB-Queries within the view for various resons. For instance views can be cached and so you don't query your database. Other people say it does not belong into the view for other reasons like better testing. So you'r free to choose or follow a path.

2) i.e. shows "["test"]" and I want it to show "test"

p.interests.collect(&:comment_text) returns an array. Erb does indirectly call p.interests.collect(&:comment_text).to_s so you get what you see.

If you want a different output you have to iterate over your comment_texts.

<% @properties.each do |property| %>
     property.interests.each do |interest|
        <td><%=interest.comment_text %>
    <% end %>
<% end %>

Upvotes: 1

Related Questions