【问题标题】:How to join on more three tables in Rails 3rails 3,加入更多三张桌子
【发布时间】:2011-05-01 05:49:40
【问题描述】:

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

我的代码

课程报价 :destroy
  has_many :cmets, :through => :usercmets, :dependent => :destroy

结束
类用户 :destroy
  has_many :cmets,:through => :usercmets, :dependent => :destroy
  has_many :offers, :dependent => :destroy

结尾
类用户注释 

类注释  :destroy
  has_one :offer, :through => :usercmets
  has_one :user, :through => :usercmets

结束

架构

create_table "offers", :force => true do |t|
  t.integer "step_id"
  t.integer "user_id"
  t.date "报价日期"
结束
create_table "users", :force => true do |t|
  t.string "名字", :limit => 100, :default => ""
  t.string "姓氏", :limit => 100, :default => ""
  t.string "email", :limit => 100
结束
create_table "usercmets", :force => true do |t|
  t.integer "user_id"
  t.integer "airoffer_id"
  t.integer "comment_id"
  t.boolean "共享"
结束
create_table "cmets", :force => true do |t|
  t.string "评论"
  t.datetime "created_at"
  t.datetime "updated_at"
结束

index.html.erb

 

???

  

在我的 html.erb 页面中,我想找到一个关于报价 (offer_id) 和用户 (user_id) 的评论。

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

【问题讨论】:

  • 请您缩进您的代码并在您的视图中包含您需要的示例。
  • 对不起马克我的不良做法,我缩进了我的代码。

标签: ruby-on-rails activerecord


【解决方案1】:

这将为您提供 User#123 和 Offer#456 的 cmets 数组

UserComment.find(:all, :conditions => {
  :user_id  => 123,
  :offer_id => 456
}).collect(&:comment)

【讨论】:

  • 谢谢你的回答,但是没有使用rails的关联模型属性的解决方案?
  • 是的,可能有,但在本例中,您根本不必触摸users 表。所有其他解决方案将users 添加到联接中,这会减慢您的查询速度。现在这似乎微不足道,但是当您的数据量增长并且查询数量增加时,您会感谢我的。
【解决方案2】:

在我看来你想要的是:

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

这有帮助吗?

【讨论】:

  • 是的,亚当,这是真的。我的分析是错误的。用户 id 必须在评论模型中,我认为是商品 id。谢谢你的帮助。
【解决方案3】:

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

我的代码

class offer &lt; ActiveRecord::Base 
  belongs_to :user
  has_many :comments, :dependent => :destroy, :order => "updated_at DESC"
end

class User &lt; ActiveRecord::Base
  has_many :comments,:dependent => :destroy
  has_many :offers, :dependent => :destroy
end 

class Comment &lt; 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

<% @offers.each do |airoffer| %>
<% airoffer.comments[0].comment %>
<% end %>

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

【讨论】:

    【解决方案4】:

    我会这样使用它:

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

    或者:

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

    Rails 有很多漂亮的方式来编写查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 2013-12-11
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多