在Rails中,您可以使用嵌套路由为has_one和has_many关系创建RESTful路由.可以在Rails指南中找到示例
我想问一下为habtm关系配置RESTful路由是否有好办法?例如,如果我有一个关系A-habtm-B,我的想法是为A has_many B配置嵌套路由,为B has_many A配置嵌套路由.这样可行吗?或者,还有更好的方法?
我有2个模型(Workout,Equipment)中有一个属于很多关系.如果我使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = 5")它,但如果我使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = null")它不会返回没有关联的记录.有任何想法吗?
ruby-on-rails has-and-belongs-to-many ruby-on-rails-3 ruby-on-rails-3.1
在Ruby on Rails 4中,假设父母有很多孩子.删除父项后,还必须删除子项.除此之外,除非儿童是孤儿,否则不得删除儿童.怎么做?
我尝试了以下内容
class Parent < ActiveRecord::Base
has_many :children, inverse_of: :parent, dependent: :destroy
end
class Child < ActiveRecord::Base
belongs_to :parent, inverse_of: :children
before_destroy :checks
private
def checks
not parent # true if orphan
end
end
Run Code Online (Sandbox Code Playgroud)
但是,使用before_destroy检查,不会删除任何内容.如果被调用的原因是因为父删除,有没有办法告诉这个方法?
要求这是一件奇怪的事吗?我的意思是,防止删除孩子.
我有两个表:categories和videos,然后有一个数据透视表,因为这是一个belongsToMany关系。
我想做的是将所有视频都归类为多个类别之一,而不是单个视频实例。
例如
我想获取不在类别2或3中的视频,这意味着它将返回视频3。
到目前为止,我一直在尝试,但并没有得到预期的结果,这是因为仍在视频1和2中找到了另一行,因为它们位于类别1中:
Video::whereHas('categories', function($query) {
$query->whereNotIn('category_id', [2,3]);
})->take(25)->get();
Run Code Online (Sandbox Code Playgroud)
从中填充的查询是:
select * from `videos` where exists (select * from `categories` inner join
`category_video` on `categories`.`id` = `category_video`.`category_id` where
`videos`.`id` = `category_video`.`video_id` and `category_id` != ? and
`category_id` != ? and `categories`.`deleted_at` is null) and `videos`.`deleted_at`
is null order by `created_at` desc limit 25
Run Code Online (Sandbox Code Playgroud) 我正在开发CakePHP 1.2应用程序.我有一个模型"用户"通过连接表定义了与其他表的几个HABTM关系.
我现在的任务是根据存储在其中一个HABTM表中的数据查找用户信息.不幸的是,当查询执行时,我的条件被拒绝,并显示有关丢失表的错误.在检查时,似乎CakePHP不包括select语句中的任何HABTM表.
我的用户HABTM关系如下:
var $hasAndBelongsToMany = array(
'Course' => array(
'className' => 'Course',
'joinTable' => 'course_members',
'foreignKey' => 'user_id',
'associationForeignKey' => 'course_id',
'conditions' => '',
'order' => '',
'limit' => '',
'uniq' => false,
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'School' => array(
'className' => 'School',
'joinTable' => 'school_members',
'foreignKey' => 'user_id',
'associationForeignKey' => 'school_id',
'conditions' => '',
'order' => '',
'limit' => '',
'uniq' => false,
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => …Run Code Online (Sandbox Code Playgroud) 一般混乱
我有乐队可以有3种类型.我在之前的SO文章中读到,处理这个问题的正确方法是几个步骤:
1)在band.rb
has_and_belongs_to_many :genres
Run Code Online (Sandbox Code Playgroud)
2)创建band_genres连接表
即使阅读了文档后,我对HABTM的实际含义感到有些困惑.我想我通常会认为"一个乐队有很多类型",没有并且属于很多.因此,对此的快速DUMBED解释将是伟大的.
与夹具混淆
另外,当我为band_genres做夹具时,我有
{
"The Reaper Band and Funk": { "band": "The Reaper Band", "genre": "Funk" },
"The Reaper Band and Rock": { "band": "The Reaper Band", "genre": "Rock" }
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个"未知"的乐队专栏.我认为rails应该知道"The Reaper Band"会指一个乐队乐队的乐队(当然是同名)并且会抓住那个id并且知道这个乐器中的"band"会引用连接表中的band_id .我宁愿我的灯具看起来像硬编码的数字.
与工厂混淆
当我在工厂创建一个乐队时,我想指定它的类型:
Factory.define :band do |f|
f.sequence(:name) { |n| "Band#{n}" }
f.mailing_lists { |mailing_lists| [mailing_lists.association(:mailing_list)] }
f.genres 2
end
Run Code Online (Sandbox Code Playgroud)
我在这里意识到我可能需要一个硬编码的genre_id.但是为什么rails不看那个并说"哦,他想把id = 2的类型添加到band_genres表".
我不期待铁路为我处理所有肮脏的工作,但我确实想按照规则行事.
我有一个产品型号,有许多部分,一个部分可以属于许多产品.
剖面模型具有Feature,Standard和Option的子类.
我的模特是:
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
has_and_belongs_to_many :sections
end
class Section < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Feature < Section
end
class Standard < Section
end
class Option < Section
end
Run Code Online (Sandbox Code Playgroud)
在我的产品控制器中,我可以这样做:
@product.sections.build
Run Code Online (Sandbox Code Playgroud)
我希望能够像这样的东西到达子类:
@product.features.build
@product.standards.build
@product.options.build
Run Code Online (Sandbox Code Playgroud)
但它只是错误的"未定义的方法'功能'"等.
请有人能告诉我怎么做吗?
Equipment.create(name: "Room to run")
Equipment.create(name: "Pull-up bar")
Workout.create(
description: "Do 100 pull-ups then run 5km",
:equipment => Equipment.where(:name => 'Pull-up bar'))
Run Code Online (Sandbox Code Playgroud)
设备和锻炼有HABTM关系.上面的种子代码有效,但我怎么能同时分配第二个设备协会?
ruby ruby-on-rails seed has-and-belongs-to-many ruby-on-rails-3
我在类别,产品和品牌之间有这种关系:
class Brand < ActiveRecord::Base
has_many :products
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
belongs_to :brand
end
Run Code Online (Sandbox Code Playgroud)
如何通过这种关系按指定品牌选择所有类别?我试试这个,但得到一个错误
b = Brand.find(1)
Category.joins(:products).where(:products => b.products)
Run Code Online (Sandbox Code Playgroud) 我找到一个解决方案有点麻烦..
Error: Call to a member function schema() on a non-object
File: /Cake/Model/Model.php
Line: 3627
Run Code Online (Sandbox Code Playgroud)
在我的数据库中有表格文章,主题标签和关联articles_hashtags与foreignkeys article_id和hashtag_id ..所以我想知道每篇文章有哪些主题标签..
我的文章模型
class Article extends AppModel {
public $hasAndBelongsToMany = array(
'Hashtag' =>
array(
'className' => 'Hashtag',
'joinTable' => 'articles_hashtags',
'foreignKey' => 'article_id',
'associationForeignKey' => 'hashtag_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'with' => ''
)
);
}
Run Code Online (Sandbox Code Playgroud)
文章管理员
class ArticleController extends …Run Code Online (Sandbox Code Playgroud) php many-to-many cakephp has-and-belongs-to-many model-associations