导轨3,连接更多三张桌子
我想在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(offer_id)和一个用户(user_id)的评论。
你可以帮帮我吗 ? 谢谢你,对我的英文表达感到抱歉,我是法国人。
这将为您提供123#用户#456用户的评论数组
UserComment.find(:all, :conditions => {
:user_id => 123,
:offer_id => 456
}).collect(&:comment)
它看起来像我想你想要的是:
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 => #)
并传入您想要的用户和优惠。
这有帮助吗?
最后,我选择了这个解决方案
我的代码
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
<%@ offers.each do | airoffer | %>
<%airoffer.comments [0] .comment%>
<%end%>
我知道,这不是更好的解决方案,但我第一次使用它。
链接地址: http://www.djcxy.com/p/35951.html上一篇: rails 3, joining on more three tables
下一篇: ActiveRecord::Base#find returns no records in Single Table Inheritance (STI)