【发布时间】:2016-01-11 16:53:21
【问题描述】:
我有以下 Ecto 架构:
schema "videos" do
field :name, :string
field :mpdName, :string
field :urlPlayready, :string, size: 500
field :urlWidevine, :string, size: 500
field :urlFile, :string, size: 500
field :custom_data, :string, size: 500
field :drm_today, :boolean
field :vote, :integer
has_many :user_videos, VideoBackend.UserVideos
has_many :users, through: [:user_videos, :user]
timestamps
end
schema "users" do
field :name, :string
field :email, :string , [:primary_key]
field :encrypted_password, :string
field :password, :string, virtual: true
has_many :user_videos, VideoBackend.UserVideos
has_many :videos, through: [:user_videos, :video]
timestamps
end
schema "user_videos" do
belongs_to :users, VideoBackend.User
belongs_to :videos, VideoBackend.Video
field :time, :float
field :device, :integer
timestamps
end
现在我想进行查询,选择给定特定“用户”和特定“视频”的 user_videos。在 SQL 中,我会编写这个简单的查询:
SELECT FROM user_videos WHERE user = "user" and video = "video"
我尝试编写以下 Ecto 查询,但它不起作用:
def single_userVideo(user,video) do
from u in UserVideos,
where: u.user == ^user and u.video == ^video
end
你知道我该如何解决吗?
【问题讨论】: