Questions tagged «has-many-through»

3
如何将记录添加到has_many:通过Rails中的关联
class Agents << ActiveRecord::Base belongs_to :customer belongs_to :house end class Customer << ActiveRecord::Base has_many :agents has_many :houses, through: :agents end class House << ActiveRecord::Base has_many :agents has_many :customers, through: :agents end 我该如何添加Agents模型Customer? 这是最好的方法吗? Customer.find(1).agents.create(customer_id: 1, house_id: 1) 上面的内容在控制台上工作正常,但是,我不知道如何在实际应用程序中实现这一点。 想象一下,为客户填写了一个表格,该表格也house_id作为输入。然后在控制器中执行以下操作? def create @customer = Customer.new(params[:customer]) @customer.agents.create(customer_id: @customer.id, house_id: params[:house_id]) @customer.save end …

5
如何使用has_many关联在FactoryGirl中建立工厂
有人可以告诉我是否只是以错误的方式进行设置吗? 我有以下具有has_many.through关联的模型: class Listing < ActiveRecord::Base attr_accessible ... has_many :listing_features has_many :features, :through => :listing_features validates_presence_of ... ... end class Feature < ActiveRecord::Base attr_accessible ... validates_presence_of ... validates_uniqueness_of ... has_many :listing_features has_many :listings, :through => :listing_features end class ListingFeature < ActiveRecord::Base attr_accessible :feature_id, :listing_id belongs_to :feature belongs_to :listing end 我正在使用Rails …

4
Rails has_many:通过在联接模型中按附加属性查找
对于Ruby和Rails来说都是新手,但是我现在受过教育(这显然没有任何意义,哈哈)。 我有两个模型,Event和User通过表EventUser加入 class User < ActiveRecord::Base has_many :event_users has_many :events, :through => :event_users end class EventUser < ActiveRecord::Base belongs_to :event belongs_to :user #For clarity's sake, EventUser also has a boolean column "active", among others end class Event < ActiveRecord::Base has_many :event_users has_many :users, :through => :event_users end 这个项目是一个日历,在其中我必须跟踪人们注册并为给定事件划掉他们的名字。我认为多对多是一种好方法,但是我不能做这样的事情: u = …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.