Rails 多对多,条件添加新条件
我用“喜欢”和“推荐”来建模用户和电影。
模型如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建
推荐
和点赞
模型,并使用它们加入电影用户。或者只是一个名为“不知道如何”的模型(RecommendLike
?),如果您确实希望保存在一张桌子上。进而:You can create
Recommendation
andLike
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: