mysql - How to Show Name Instead of Id in Rails -


this problem has me stumped. i'm still new @ ror , learning.

i have 2 tables: countertops , countmaterial. user select features countertop including material type. options material listed in countmaterial table , selection collection.

my question once selection made , countertop created how display name of material type on index page countertops instead of countertype, integer generated match name in countmaterial table?

i'd rather index display "granite" instead of "1", example. "granite" listed in countmaterial table , when user selects "granite", populates countertop table "1" in countertype column. marble "2" , on...

here's schema:

create_table "countertops", force: :cascade |t|  t.string   "size"  t.string   "color"  t.datetime "created_at",  null: false  t.datetime "updated_at",  null: false  t.string   "zipcode"  t.string   "countertype" end  create_table "countmaterials", force: :cascade |t|  t.string   "name"  t.datetime "created_at",    null: false  t.datetime "updated_at",    null: false  t.integer  "countertop_id" end 

my countertop controller index:

def index  @countertops = countertop.all  @countertops = countertop.includes(:countmaterial).all end 

my index code:

<% @countertops.each |countertop| %>   <tr>     <td><%= countertop.zipcode %></td>     <td><%= countertop.countmaterial.name %></td> 

associations:

class countertop < activerecord::base   has_one :countmaterial end  class countmaterial < activerecord::base   belongs_to :countertop end 

what folks think??

you're going confused specific model names; when naming models , controllers - keep super simple. 1 word...

#app/models/counter.rb class counter < activerecord::base    #columns id | type_id | material_id | size_id | color_id | zip_code| created_at | updated_at    belongs_to :type    belongs_to :material    belongs_to :size    belongs_to :color    delegate :name, to: :size, prefix: true end  #app/models/option.rb class option < activerecord::base    #columns id | type | name | created_at | updated_at    has_many :counters end  #app/models/size.rb class size < option end  #app/models/type.rb class type < option end  #app/models/color.rb class color < option end  #app/models/material.rb class material / option end 

this give ability following:

#config/routes.rb resources :counters  #app/controllers/counters_controller.rb class counterscontroller < applicationcontroller    def index       @counters = counter.all    end end  #app/views/counters/index.html.erb <% @counters.each |counter| %>    <%= counter.size_name %> <% end %> 

to give context on how works, need know rails & ruby object orientated. might not mean lot, it's vitally important when developing apps them.

object orientated programming pattern puts object @ center of code. when understand how works, nothing ever same...

enter image description here

in "traditional" programming, work user flow. known event driven programming, , although works standard apps, not suit ruby/rails environment.

web apps have capacity handle more data / functionality makes perfect sense treat object.

thus, whenever deal ruby, have think everything perspective of objects you're trying crud (create read update destroy).

this why countertop model little sketchy - what's object you're trying invoke?

once see object sits @ core of how rails works, you'll able construct around it, above.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -