Tag: 有很多通过

Rails has_many:通过嵌套表单

我刚刚跳进了has_many :through联想。 我正在尝试通过单一表单实现为所有3个表( Physician , Patient和关联表)保存数据的function。 我的迁移: class CreatePhysicians < ActiveRecord::Migration def self.up create_table :physicians do |t| t.string :name t.timestamps end end end class CreatePatients < ActiveRecord::Migration def self.up create_table :patients do |t| t.string :name t.timestamps end end end class CreateAppointments < ActiveRecord::Migration def self.up create_table :appointments do |t| t.integer :physician_id t.integer :patient_id t.date :appointment_date […]

Rails 3,嵌套的多级表单和has_many通过

我试图让它工作,但它不是! 我有 class User :event_users has_many :event_users accepts_nested_attributes_for :event_users end class Event :event_users accepts_nested_attributes_for :users end class EventUser < ActiveRecord::Base set_table_name :events_users belongs_to :event belongs_to :user accepts_nested_attributes_for :events accepts_nested_attributes_for :users end 还有表格布局 event_users user_id event_id user_type events id name users id name 这是我的forms ‘participating’ %> 问题是如果我以这种方式创建一个新用户,它不会设置user_type的值(但它会创建一个用户和一个带有user_id和event_id的event_users)。 如果我在创建用户并提交后返回编辑表单,则在events_users中设置user_type的值。 (我也尝试过没有formtastic)任何建议? 谢谢! – – 编辑 – – 我也尝试过在用户之前使用event_users […]

rspec测试has_many:through和after_save

我有一个(我认为)相对简单的has_many :through与连接表的关系: class User :user_following_thing_relationships end class Thing :user_following_thing_relationships, :source => :user end class UserFollowingThingRelationship < ActiveRecord::Base belongs_to :thing belongs_to :user end 这些rspec测试(我知道这些不一定是好的测试,这些只是为了说明发生了什么): describe Thing do before(:each) do @user = User.create!(:name => “Fred”) @thing = Thing.create!(:name => “Foo”) @user.things << @thing end it "should have created a relationship" do UserFollowingThingRelationship.first.user.should == @user UserFollowingThingRelationship.first.thing.should == @thing […]