Rails Model Constants -


i have attachments , category models when user uploads file, can select category attachment. want categories static now. advice on how create static category model options? have right following error: undefined method 'title' syllabus":string

category model

class category < activerecord::base   category = ['syllabus', 'assignments', 'handouts', 'lectures', 'other']   has_many :attachments end 

attachment new.html.erb

<%= simple_form_for([@group, @group.attachments.build]) |f| %>    <%= f.collection_select :category_id, category::category, :id, :title, { promt: "choose category" } %>  <%= f.submit %>  <% end %> 

attachment model

class attachment < activerecord::base   belongs_to :user   belongs_to :group   belongs_to :category end 

schema

 create_table "categories", force: :cascade |t|     t.string   "title"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end   create_table "attachments", force: :cascade |t|     t.string   "title"     t.datetime "created_at",  null: false     t.datetime "updated_at",  null: false     t.integer  "user_id"     t.string   "name"     t.integer  "group_id"     t.integer  "category_id"  end 

you seem want have fixed list of categories , yet, presumably future expansion, want refer via belongs_to , category_id in attachments model. can't know in advance category_id values database engine, or else's, might assign category objects created real.

you're getting error because collection_select expecting collection give consist of real category model instances have methods #title arising associated database table's attributes. instead, you're trying give array of strings.

therefore, should use seed data - google "rails 4 seed data" idea of do. seed data, you'll create real database instances of category model loaded part of application's installation phase. model won't have editing/management interface in versions of application, real instances of in database nonetheless.

[edit: prefer rake db:create db:migrate approach bringing applications, since db:schema:load may not work if esoteric id column constructions occurred in migration files, schema.rb summary file may not have accurately recorded due rails bugs. accordingly, prefer use approach described in old stackoverflow question's answer - add rows on migrations - ymmv, may prefer things seeds.rb instead.]


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -