rails 3, joining on more three tables
I would like joining more 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
schema
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 %>
and in my html.erb page i would like find a comment for an offer (offer_id) and an user (user_id).
Could you help me ? thank you and sorry for my english expression, i am french.
这将为您提供123#用户#456用户的评论数组
UserComment.find(:all, :conditions => {
:user_id => 123,
:offer_id => 456
}).collect(&:comment)
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?
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.
链接地址: http://www.djcxy.com/p/35952.html上一篇: 未定义的方法'密码='为#<用户:0xb88ac38>
下一篇: 导轨3,连接更多三张桌子