我想在 rails 3 中加入更多的三个表
我的代码
class offer < ActiveRecord::Base
归属地:用户
has_many :usercomments, :dependent => :destroy
has_many :comments, :through => :usercomments, :dependent => :destroy
结尾
类用户 < ActiveRecord::Base
has_many :usercomments, :dependent =>:destroy
has_many :comments,:through => :usercomments, :dependent => :destroy
has_many :offers, :dependent => :destroy
结尾
类 Usercomment < ActiveRecord::Base
归属地:用户
归属地:评论
归属地:提供
结尾
class Comment < ActiveRecord::Base
has_one :usercomment, :dependent => :destroy
has_one :offer, :through => :usercomments
has_one :user, :through => :usercomments
结尾
模式
create_table "offers", :force => true do |t|
t.整数“step_id”
t.integer "user_id"
t.date "offerdate"
结尾
create_table "users", :force => true do |t|
t.string "firstname", :limit => 100, :default => ""
t.string "lastname", :limit => 100, :default => ""
t.string "email", :limit => 100
结尾
create_table "usercomments", :force => true do |t|
t.integer "user_id"
t.integer "airoffer_id"
t.integer "comment_id"
t.boolean “共享”
结尾
create_table "comments", :force => true do |t|
t.string "评论"
t.datetime "created_at"
t.datetime "updated_at"
结尾
index.html.erb
<% airoffers.each do |airoffer| %>
???
<%结束%>
在我的 html.erb 页面中,我想找到对报价 (offer_id) 和用户 (user_id) 的评论。
你可以帮帮我吗 ?谢谢你,对不起我的英语表达,我是法国人。
最佳答案
在我看来,您想要的是:
class User < ActiveRecord::Base
has_many :comments
has_many :offers
end
class Offer < ActiveRecord::Base
has_many :comments
belongs_to :user
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :offer
end
如果您想要属于特定用户和特定优惠的所有评论,只需执行
Comment.where(:user_id => # :offer_id => #)
并传入您想要的用户和优惠。这有帮助吗?
关于ruby-on-rails - rails 3,加入更多三个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4032269/