导轨3,连接更多三个桌子

时间:2010-10-27 10:34:40

标签: ruby-on-rails activerecord

我想在rails 3中加入更多三个表

我的代码

class offer < ActiveRecord::Base 

  belongs_to :user
  has_many :usercomments, :dependent => :destroy
  has_many :comments, :through => :usercomments, :dependent => :destroy

end
class User < ActiveRecord::Base

  has_many :usercomments, :dependent =>:destroy
  has_many :comments,:through => :usercomments, :dependent => :destroy
  has_many :offers, :dependent => :destroy

end 
class Usercomment < ActiveRecord::Base

  belongs_to :user
  belongs_to :comment
  belongs_to :offer

end
class Comment < ActiveRecord::Base

  has_one :usercomment, :dependent => :destroy
  has_one :offer, :through => :usercomments
  has_one :user, :through => :usercomments

end

模式

create_table "offers", :force => true do |t|
  t.integer  "step_id"  
  t.integer  "user_id"  
  t.date     "offerdate"  
end
create_table "users", :force => true do |t|  
  t.string   "firstname",            :limit => 100, :default => ""  
  t.string   "lastname",             :limit => 100, :default => ""  
  t.string   "email",                :limit => 100  
end
create_table "usercomments", :force => true do |t|
  t.integer  "user_id"
  t.integer  "airoffer_id"
  t.integer  "comment_id"
  t.boolean  "shared"
end 
create_table "comments", :force => true do |t|
  t.string   "comment" 
  t.datetime "created_at"
  t.datetime "updated_at"
end

index.html.erb

 <% airoffers.each do |airoffer| %>

???

 <% end %> 

在我的html.erb页面中,我想查找商品(offer_id)和用户(user_id)的评论。

你可以帮帮我吗?谢谢,对不起我的英语表达,我是法国人。

4 个答案:

答案 0 :(得分:0)

这将为用户#123和#456

提供一系列评论
UserComment.find(:all, :conditions => {
  :user_id  => 123,
  :offer_id => 456
}).collect(&:comment)

答案 1 :(得分:0)

在我看来,你想要的是:

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 => #)并传入您想要的用户和优惠。

这有帮助吗?

答案 2 :(得分:0)

最后,我选择了这个解决方案

我的代码

class offer < ActiveRecord::Base 

  belongs_to :user
  has_many :comments, :dependent => :destroy, :order => "updated_at DESC"

end
class User < ActiveRecord::Base

  has_many :comments,:dependent => :destroy
  has_many :offers, :dependent => :destroy

end 
class Comment < ActiveRecord::Base

  has_one :user, :dependent => :destroy
  has_one :airoffer, :dependent => :destroy

end

模式

create_table "offers", :force => true do |t|
  t.integer  "user_id"  
  t.date     "offerdate"  
end
create_table "users", :force => true do |t|  
  t.string   "firstname",            :limit => 100, :default => ""  
  t.string   "lastname",             :limit => 100, :default => ""  
  t.string   "email",                :limit => 100  
end
create_table "comments", :force => true do |t|
  t.integer  "user_id"
  t.integer  "offer_id"  
  t.string   "comment" 
  t.datetime "created_at"
  t.datetime "updated_at"
end
在offer_controller.rb中

@offers = User.find(current_user.id).offers.includes(:comments)

和我的index.html.erb

&lt;%@ offers.each do | airoffer | %GT;

&lt;%airoffer.comments [0] .comment%&gt;

&lt;%end%&gt;

我知道,这不是更好的解决方案,但是我第一次使用它。

答案 3 :(得分:0)

我会这样用它:

Comment.find( 
  :all, 
  :conditions => {
    :user_id  => 123,
    :offer_id => 456
  },
  :join => :usercomment
)

OR:

Comment.find( 
  :all, 
  :conditions => [
    "usercomments.user_id = ? AND usercomments.offer_id = ?",
    123,
    456
  ],
  :join => :usercomment
)

Rails有很多很好的方法来编写查询。