如何script/generate migration
为has_and_belongs_to_many
关系创建连接表?
该应用程序在Rails 2.3.2上运行,但我也安装了Rails 3.0.3.
migration code-generation ruby-on-rails has-and-belongs-to-many
鉴于以下内容
class User < ActiveRecord::Base
has_and_belongs_to_many :companies
end
class Company < ActiveRecord::Base
has_and_belongs_to_many :users
end
Run Code Online (Sandbox Code Playgroud)
您如何为公司和用户定义工厂,包括双向关联?这是我的尝试
Factory.define :company do |f|
f.users{ |users| [users.association :company]}
end
Factory.define :user do |f|
f.companies{ |companies| [companies.association :user]}
end
Run Code Online (Sandbox Code Playgroud)
现在我试试
Factory :user
Run Code Online (Sandbox Code Playgroud)
也许不出所料,这会导致无限循环,因为工厂递归地使用彼此来定义自己.
更令人惊讶的是,我没有提到如何在任何地方做到这一点,是否有一种模式来定义必要的工厂或我做了一些从根本上错误的事情?
ruby-on-rails associations has-and-belongs-to-many factory-bot
我有两个模型restaurant
和user
我要执行has_and_belongs_to_many关系.
我已经进入模型文件并添加了has_and_belongs_to_many :restaurants
和has_and_belongs_to_many :users
我假设在这一点上我应该能够像Rails 3那样做:
rails generate migration ....
Run Code Online (Sandbox Code Playgroud)
但我所尝试的一切似乎都失败了.我确信这是非常简单的我对rails很新,所以我还在学习.
migration ruby-on-rails has-and-belongs-to-many ruby-on-rails-3
如何在不删除项目本身的情况下删除HABTM关联项目?
例如,假设我有3名学生一起参加科学课程.如何从StudentsClasses表中删除Science对象而不删除实际的Science参考?我猜这 Student.Classes.first.delete
不是一个好主意.
我使用拖放式JavaScript进行添加和删除,而不是复选框.有什么想法吗?
我过去已经建立了这种HABTM关系并且它之前有效....现在它不是,我在我的智慧结束时试图弄清楚什么是错的.我整天都在看导轨指南,似乎无法弄清楚我做错了什么,所以真的很感激帮助.
我有2个模型通过连接模型连接,我试图找到基于相关模型的属性的记录.
Event.rb
has_and_belongs_to_many :interests
Run Code Online (Sandbox Code Playgroud)
Interest.rb
has_and_belongs_to_many :events
Run Code Online (Sandbox Code Playgroud)
以及创建的连接表迁移
create_table 'events_interests', :id => false do |t|
t.column :event_id, :integer
t.column :interest_id, :integer
end
Run Code Online (Sandbox Code Playgroud)
我试过了
@events = Event.all(:include => :interest, :conditions => [" interest.id = ?", 4 ] )
Run Code Online (Sandbox Code Playgroud)
但得到错误"没有找到名为'兴趣'的协会;也许你拼错了它?"......当然我没有
我试过了
@events = Event.interests.find(:all, :conditions => [" interest.id = ?", 4 ] )
Run Code Online (Sandbox Code Playgroud)
但得到错误"#Class的未定义方法`兴趣':0x4383348"
我怎样才能找到兴趣ID为4的事件....我肯定会从这个哈哈发出光头
我正在开发一个用于为购物网站创建特价的功能.一种产品可以有多种特殊产品,显然特殊产品可以有多种产品.
我正在使用一段has_and_belongs_to_many
关系,所以我宣布:
Product.rb
has_and_belongs_to_many :specials
Run Code Online (Sandbox Code Playgroud)
Special.rb
has_and belongs_to_many :products
Run Code Online (Sandbox Code Playgroud)
现在,通过产品@product
和特殊产品,@special
可以创建一个类似的关联.
@special.products << @product
Run Code Online (Sandbox Code Playgroud)
执行此操作后,以下情况属实:
@special.products.first == @product
Run Code Online (Sandbox Code Playgroud)
而且,重要的是:
@product.specials.first == @special
Run Code Online (Sandbox Code Playgroud)
当我删除使用此关联
@special.products.delete(@product)
Run Code Online (Sandbox Code Playgroud)
然后@product
从特价中删除,换句话说@special.products.first==nil
,@product
仍然包含 @special
@products.specials.first==@special
除了编写删除方法之外,还有什么方法可以在一次调用中执行此操作吗?
我有两个模型,用户和促销活动.这个想法是促销可以有很多用户,用户可以有很多促销.
class User < ActiveRecord::Base
has_and_belongs_to_many :promotions
end
class Promotion < ActiveRecord::Base
has_and_belongs_to_many :users
end
Run Code Online (Sandbox Code Playgroud)
我还有一个promotion_users表/模型,没有自己的id.它引用了user_id和promotions_id
class PromotionsUsers < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
那么,如何将用户添加到促销中?我尝试过这样的事情:
user = User.find(params[:id])
promotion = Promotion.find(params[:promo_id])
promo = user.promotions.new(promo)
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
NoMethodError: undefined method `stringify_keys!' for #<Promotion:0x10514d420>
Run Code Online (Sandbox Code Playgroud)
如果我尝试这一行:promo = user.promotions.new(promo.id)
我收到此错误:
TypeError: can't dup Fixnum
Run Code Online (Sandbox Code Playgroud)
我确信我的问题有一个非常简单的解决方案,而我只是没有以正确的方式寻找解决方案.
HABTM关系不支持该:dependent
选项是真的吗?
class Person < ActiveRecord::Base
has_and_belongs_to_many :posts, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
我正在尝试rails edge.
虽然代码似乎是正确的,但当我尝试发送表单时,多次选择的值不会被发送.
如果我只删除多个选项,一切都按预期工作,只考虑一个值,但每个事务存储多个标记很重要.
模型
Transaction.rb
class Transaction < ActiveRecord::Base
has_and_belongs_to_many :tags
Run Code Online (Sandbox Code Playgroud)
Tag.rb
class tag < ActiveRecord::Base
has_and_belongs_to_many :transactions
Run Code Online (Sandbox Code Playgroud)
视图
<%= form.collection_select :tag_ids, @tags, :id, :name, {},
{:multiple => true} %>
Run Code Online (Sandbox Code Playgroud)
结果:
<select id="transaction_tag_ids" multiple="multiple" name="transaction[tag_ids][]">
<option value="1">..</option>
</select>
Run Code Online (Sandbox Code Playgroud)