Rails 3 connecting more than three tables

I would like to join more than three tables in rails 3

my code

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

scheme

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).

? , .

+5
4

# 123 en # 456

UserComment.find(:all, :conditions => {
  :user_id  => 123,
  :offer_id => 456
}).collect(&:comment)
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 => #) .

?

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

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 this is not the best solution, but for the first time I will use it.

0
source

I would use it like this:

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 has many beautiful ways to write queries.

0
source

All Articles