0

我的模型是用户、练习和写作。

class User < ActiveRecord::Base
    has_many :exercises, :through => :writings
    has_many :writings
end

class Exercise < ActiveRecord::Base
    has_many :users, :through => :writings
    has_many :writings
end

class Writing < ActiveRecord::Base
    belongs_to :user
    belongs_to :exercise
    attr_accessible :writing_date, :exercise_id
end

我的数据库/schema.rb

 ActiveRecord::Schema.define(:version => 20120517142448) do
   create_table "exercises", :force => true do |t|
     t.string   "etitle"
     t.text     "ebody"
     t.decimal  "average",             :precision => 3, :scale => 1
     t.datetime "created_at"
     t.datetime "updated_at"
   end
   create_table "users", :force => true do |t|
     t.string   "email",                                 :default => "",   :null => false
     t.datetime "created_at"
     t.datetime "updated_at"
     t.integer  "user_kind",                             :default => 0
   end
   create_table "writings", :force => true do |t|
     t.integer  "user_id"
     t.integer  "exercise_id"
     t.date     "writing_date"
     t.datetime "created_at"
     t.datetime "updated_at"
   end

连接表正在写入。我尝试在每个练习视图(app/view/exercises/index.html.erb)上显示她的写作日期。每个user_id 通过写表有很多exercise_id。Writing 表上的每条记录都有writing_id、user_id、executive_id 和writing_date。在控制台中专门为executive_id我做了这样的:

1.9.2p290 :001 > @exercise = Exercise.find_by_id(9)
=> #<Exercise id: 9, etitle: "PLS51-2012-E01", ebody: "q", average: #<BigDecimal:53eeba8,'0.1E2',9(18)>, created_at: "2012-05-20 14:31:07", updated_at: "2012-05-20 14:34:27", askisi_file_name: nil, askisi_content_type: nil, askisi_file_size: nil, askisi_updated_at: nil> 
1.9.2p290 :002 > @exercise.writings
=> [#<Writing id: 6, user_id: 1, exercise_id: 9, writing_date: "2012-06-29", created_at: "2012-05-20 14:36:45", updated_at: "2012-05-20 14:36:45">, #<Writing id: 12, user_id: 7, exercise_id: 9, writing_date: "2012-06-20", created_at: "2012-05-20 14:40:12", updated_at: "2012-05-20 14:40:12">, #<Writing id: 13, user_id: 7, exercise_id: 9, writing_date: "2012-05-02", created_at: "2012-05-20 15:41:54", updated_at: "2012-05-20 15:41:54">] 
1.9.2p290 :003 > @exercise.writings.order("writing_date ASC")
=> [#<Writing id: 13, user_id: 7, exercise_id: 9, writing_date: "2012-05-02", created_at: "2012-05-20 15:41:54", updated_at: "2012-05-20 15:41:54">, #<Writing id: 12, user_id: 7, exercise_id: 9, writing_date: "2012-06-20", created_at: "2012-05-20 14:40:12", updated_at: "2012-05-20 14:40:12">, #<Writing id: 6, user_id: 1, exercise_id: 9, writing_date: "2012-06-29", created_at: "2012-05-20 14:36:45", updated_at: "2012-05-20 14:36:45">] 
1.9.2p290 :004 > @exercise.writings.order("writing_date ASC").last
 => #<Writing id: 6, user_id: 1, exercise_id: 9, writing_date: "2012-06-29", created_at: "2012-05-20 14:36:45", updated_at: "2012-05-20 14:36:45"> 

我需要范围吗?

提前致谢!

4

1 回答 1

0

最后,我的问题的解决方案可以帮助遇到同样情况的人,这很简单。文件 app/view/exercises/index.html.erb 代码:

 <%= exercise.writings.last.try(:writing_date) %>
于 2012-06-05T22:05:47.620 回答