代码,当我喜欢post时执行
// auth()->id() is the id of the user that liked the post
$post->post_likes()->associate(auth()->id());
$post->save();
代码运行后发生的错误
方法说明\Database\Query\Builder::associate不存在。
-
Post模型
class Post extends Model
{
public function post_likes()
{
更具体地说,“如何验证一个模型是否至少需要创建x个有效的关联模型?”。我一直试图验证以与父模型相同的形式创建的嵌套模型(并最终显示即时验证( la jQuery) )。作为一个流行的示例,让我们假设以下模型和模式。
class Project
include DataMapper::Resource
property :id, Serial
property :title, String, :nullable => false
has 2..n, :tasks
end
class Task
include DataMapper::Resource
如果父模型的子模型适合某些验证,比如属性之和等于来值,那么我如何创建父模型呢?
我有一个父母的模型:
class Foo < ApplicationRecord
has_many :bars, dependent: :destroy
accepts_nested_attributes_for :bars, allow_destroy: true
end
儿童模型是:
class Bar < ApplicationRecord
belongs_to :foo
end
有什么正确的方法吗?我应该在哪里验证儿童模型?另外,我如何用rspec测试它?是像这样吗?
be
是否可以在嵌套模型上设置面搜索?我有一个用户模型,它有一个配置文件模型。我可以搜索用户模型中的术语配置文件模型。现在,我希望通过配置文件模型中的位置属性过滤搜索结果(用户)。
用户模型:
class User< ActiveRecord::Base
has_one :profile
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :profiles do
indexes :first_name
indexes :last_name
型号:
class User
include MongoMapper::Document
many :properties
validates_associated :properties
...
end
class Property
include MongoMapper::Document
belongs_to :user
many :services
validates_associated :services
...
end
class Service
include MongoMapper::Document
belongs_to :p
我有一个用户模型,它有许多已完成的任务。此外,我有用户与用户的关系,订阅,以下等行动。我正在尝试呈现所有用户完成的任务,以及用户正在跟踪的已完成任务。问题是,当我将后面的用户完成任务添加到用户完成的任务数组中时,user_id列将从后面的用户id更改为用户id。这很奇怪,也不知道是什么导致了这一切。如果我不能解释清楚的话很抱歉。
这是密码:
user.rb
class User < ActiveRecord::Base
include BCrypt
before_create :generate_auth_token!
has_secure_password :valid
我创建了两个类和教师模型,我想使用代码优先方法配置多到多个关系,并使用两个模型。当我将信息添加到类和教师表中时,使用多到多关系实体框架代码优先方法将值不插入到TeacherClasses表中。
下面是模型和控制器的代码。
这是类模型
public class Class
{
[Key]
public int ClassId { get; set; }
[Required]
public string ClassName { get; set; }
public virtual ICollection<Teacher> Teachers {
考虑下面的Mongoid模型
class Doc
include Mongoid::Document
field :name, type: String
embeds_many :images
embeds_many :videos
end
class Image
include Mongoid::Document
field :url, type: String
field :caption, type: String
embedded_in :Doc
end
class Video
include Mongoid::Document
field
我想通过FactoryGirl指定以下类型的关联。我有三种模型A、B和C,其中DataMapper中的C模型如下:
Class C do
include DataMapper::Resource
belongs_to :A, :key=>true
belongs_to :B, :key=>true
end
我不知道如何在FactoryGirl中指定它,我的意思是我想这样写:
factory :c do |c|
<To be Filled>
end
请帮帮忙。
我的rails应用程序中有一个mongoid模型,如下所示
class Groups::Post
include Mongoid::Document
include Mongoid::Attributes::Dynamic
include Mongoid::Timestamps
end
我想按以下方式修改这个模型
class Post
include Mongoid::Document
include Mongoid::Attributes::Dynamic
include Mongoid::Timestamps
end
如果我简单地更改模型名称,我将失去数据,因为mo
当一个实体需要在它的一个方法中创建其他实体时,你如何处理这种情况?我的问题是,由于每个单独的实体都没有访问ObjectContext对象,也就是使用AddToBlahs()方法的实体,所以它不能这样做。
例如,具有一个站点模型,该模型具有一个UpdateLinks()方法,该方法用于创建属于该站点的链接对象。UpdateLinks()方法没有ObjectContext。你的工作是什么?是否将一个传递给它,如下所示:
public void UpdateLinks(ProjectEntities db) {
foreach (var link in FetchLinks()) {
我有三种模式:用户、语言和角色。用户模型包含语言和角色的外键。用户(belongs_to: language)和(belongs_to: role)。语言和角色(has_many:用户)
class User < ActiveRecord::Base
belongs_to :language
belongs_to :role
end
class Language < ActiveRecord::Base
has_many :users
end
class Role < ActiveRecord::Base
has_many :users
end
在_fo
我正在为我的阿波罗服务器编写一个解析器(使用后缀),并且很难理解这种反向关联的决心是如何工作的.我对后缀查询中的include是如何工作的感到非常困惑。
我的模范协会:
User.hasOne(models.Profile) <- so the foreign key 'userId' is on the profile table
Profile.belongsTo(models.User) <- foreign key 'userId' still on profile table
我的graphql架构:
type Profile {
我有两个实体--用户和部门,这两个实体(OneToMany和ManyToOne)之间存在双向关系,我在一个服务类中管理该关系,该服务类处理向部门删除和添加用户,如下所示:
List<User> originalUserList = userJpaRepository.findAllByDepartment(department);
for (Iterator<User> iterator = originalUserList.iterator(); iterator.hasNext();) {
User user = iterator.next();
i
我有一些叫做节点的东西,它们是与HABTM相关的。每个单独的关系都由前端的用户指定.
在创建新节点时,前端UI以下列形式发送数据:
$exampleData = [
'Node' => [
'title' => 'The title of a new node',
'body' => 'The body of a new node'],
'NodeRequireNode' => [ // This is the HABTM-relation
0
我是DataMapper的新手,我正在尝试为以下场景创建模型:
我有许多用户(具有用户名、密码等),他们也可以是球员或裁判,或者两者兼而有之(所以不能选择单表继承)。基本模型为:
class User
include DataMapper::Resource
property :id, Serial
# Other user properties go here
end
class Player
include DataMapper::Resource
property :id, Serial
# Other player properties go here