2
私はRails 3.2アプリケーションでhas_many:through関係を設定しています。リレーションシップが作成されたときに、ジョインテーブルの属性に値を追加する方法がわからないことを除いて、ほとんどすべての機能が動作しています。ここで作成時にhas_many:throughアソシエーションの結合テーブルレコードに属性値を追加
は、モデルは(がチェックインテーブルの上にをsource_idを気づか)は次のとおりです。
create_table "users", :force => true do |t|
t.integer "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "checkins", :force => true do |t|
t.integer "user_id"
t.integer "location_id"
t.integer "source_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "locations", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
ここでは関係のセットアップです:私は本質的に(これらの命令を使用してい
class User < ActiveRecord::Base
has_many :checkins
has_many :locations, :through => :checkins
end
class Location < ActiveRecord::Base
has_many :checkins
has_many :users, :through => :checkins
end
class Checkin < ActiveRecord::Base
belongs_to :location
belongs_to :user
end
)を使用してユーザーと場所をロードし、チェックインとの関係を作成します。
source_id = 10
@user = User.first
@location = Location.first
@user.locations << @location
だから、私の質問はどのように私もこのラインを使用するときがチェックインテーブルに値をsource_idを追加する、次のとおりです。
@user.locations << @location
は、私はまた、より良いプロセス上の提案に開いています私は上記の持っているものよりも、この関係を持つ新しいユーザーのチェックインを作成する(私はがとを使用方法を構築作成見てきましたが、どれも私のために働くように見えません)
これはうまくいきます。あなたはおそらく、has_many:through associationのためにこの操作(または単に基本的なCRUD)を詳述する文書に私を導くことができますか?私が見つけたRailsガイドは、実際にCRUDメソッドを使うのではなく、どのように設定するかを教えてくれます。 – wrburgess
@wrburgess has_many:through associationのCRUDについて説明したリソースを今までに見つけましたか? – thomasvermaak
本当にありません:(まだ見ています – wrburgess