Tuesday, 27 August 2013

Belongs To Association Table Schema Ruby on Rails

Belongs To Association Table Schema Ruby on Rails

I want each student to be able to post multiple messages on my site.
therefore each student has_many :posts and a post belongs_to :student (one
student only)
The thing is I can create a record for a student in rails console but
can't assign a post to the student ? I am a bit confused. The student
model with the has many does not have the attributes from the belongs to
model ?
I have a student.rb model
class Student < ActiveRecord::Base
attr_accessible :first_name, :last_name, :email, :gender, :number,
:college, :password, :budget, :picture
mount_uploader :picture, PictureUploader
has_many :posts
end
I have a post.rb model
class Post < ActiveRecord::Base
attr_accessible :message
belongs_to :student
end
this is my schema
ActiveRecord::Schema.define(version: 20130827191617) do
# These are extensions that must be enabled in order to support this
database
enable_extension "plpgsql"
create_table "posts", force: true do |t|
t.text "message"
end
create_table "students", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "number"
t.string "college"
t.string "password"
t.float "budget"
t.string "picture"
t.datetime "created_at"
t.datetime "updated_at"
end

No comments:

Post a Comment