开发者

How to join on more three tables in Rails 3

开发者 https://www.devze.com 2023-01-22 08:35 出处:网络
I would like to join more than three tables in Rails 3. My code is class offer < ActiveRecord::Base

I would like to join more than three tables in Rails 3.

My code is

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

My schema is:

create_table "offers", :force => true do |t|
  t.integer  "step_id"  
  t.int开发者_StackOverflow社区eger  "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

and index.html.erb is:

 <% airoffers.each do |airoffer| %>

???

 <% end %> 

In my html.erb page I would like to find a comment for an offer (offer_id) and an user (user_id).


It looks to me like what you want is:

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

If you want all the Comments that belong to a specific User and a specific Offer, just do Comment.where(:user_id => # :offer_id => #) and pass in the User and Offer you want.

Does this help?


This will give you an array of comments for User#123 en Offer#456

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


I would use it this way:

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 have a lot of beautiful ways to write queries.


Finally, i chose this solution

my code

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

schema

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

in offer_controller.rb

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

and in my index.html.erb

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

I know, it's not the better solution, but in the first time i will use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号