我希望每个学生都能在我的网站上发布多条消息。
因此每个学生 has_many :posts 和一个 post belongs_to :student (仅限一名学生)
问题是我可以在 Rails 控制台中为学生创建记录,但不能为学生分配帖子?我有点困惑。学生模型有很多从属于模型没有的属性?
我有一个 student.rb 模型
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
我有一个 post.rb 模型
class Post < ActiveRecord::Base
attr_accessible :message
belongs_to :student
end
这是我的架构
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