Rails 多对多,条件添加新条件

发布于 2024-11-26 23:58:15 字数 1498 浏览 1 评论 0原文

我用“喜欢”和“推荐”来建模用户和电影。

模型如下:

class Movie < ActiveRecord::Base
  attr_accessible :title, :imbd_id

  has_and_belongs_to_many :liked_by, :class_name => "User", 
    :conditions => { "type" => "like" }
  has_and_belongs_to_many :recommended_by, :class_name => "User", 
    :conditions => { "type" => "recommend" }

end

class User < ActiveRecord::Base

  has_and_belongs_to_many :movies_liked, :class_name => "Movie", 
    :conditions => { "type" => "like" }
  has_and_belongs_to_many :movies_recommended, :class_name => "Movie", 
    :conditions => { "type" => "recommend" }

end

用一个表来映射关系:

 create_table "movies_users", :id => false, :force => true do |t|
    t.integer  "user_id"
    t.integer  "movie_id"
    t.string   "type"           # this can be "like" or "recommend"
    t.string   "status_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

现在,当我尝试处理

u = User.first
m = Movie.first
u.movie_liked << m 

以下错误

ruby-1.9.2-p290 :003 > u.movies_tweeted << m 
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: movies.type: SELECT * FROM "movies" INNER JOIN "movies_users" ON "movies".id = "movies_users".movie_id WHERE ("movies_users".user_id = 1  AND ("movies"."type" = 'like'))

时,我收到一个错误任何想法我都可以很好地构造它,这样我就可以使用 <<运算符并且它会自动分配正确的类型?

谢谢!

I am modelling users and movies with "liked" and "recommeded".

The models are as follows:

class Movie < ActiveRecord::Base
  attr_accessible :title, :imbd_id

  has_and_belongs_to_many :liked_by, :class_name => "User", 
    :conditions => { "type" => "like" }
  has_and_belongs_to_many :recommended_by, :class_name => "User", 
    :conditions => { "type" => "recommend" }

end

class User < ActiveRecord::Base

  has_and_belongs_to_many :movies_liked, :class_name => "Movie", 
    :conditions => { "type" => "like" }
  has_and_belongs_to_many :movies_recommended, :class_name => "Movie", 
    :conditions => { "type" => "recommend" }

end

with one table to map the relationship:

 create_table "movies_users", :id => false, :force => true do |t|
    t.integer  "user_id"
    t.integer  "movie_id"
    t.string   "type"           # this can be "like" or "recommend"
    t.string   "status_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Now I am getting an error when I try to do

u = User.first
m = Movie.first
u.movie_liked << m 

with the following error

ruby-1.9.2-p290 :003 > u.movies_tweeted << m 
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: movies.type: SELECT * FROM "movies" INNER JOIN "movies_users" ON "movies".id = "movies_users".movie_id WHERE ("movies_users".user_id = 1  AND ("movies"."type" = 'like'))

Any idea I can structure it nicely so I can just use the << operator and it will automatically assign the correct type?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2024-12-03 23:58:15

您可以创建推荐点赞模型,并使用它们加入电影用户。或者只是一个名为“不知道如何”的模型(RecommendLike?),如果您确实希望保存在一张桌子上。进而:

User
  has_many :recommendations
  has_many :movies_recommended, :class_name => "Movie", :through => :recommendations
  has_many :likes
  has_many :movies_liked, :class_name => "Movie", :through => :likes

You can create Recommendation and Like models and use them to join the Users with Movies. Or just a single model called don't know how(RecommendLike?), if you really wish to save on one table. And then:

User
  has_many :recommendations
  has_many :movies_recommended, :class_name => "Movie", :through => :recommendations
  has_many :likes
  has_many :movies_liked, :class_name => "Movie", :through => :likes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文