Rails 자체의 many to many 관계 self hasmany
4578 단어 ActiveRecordrails3has_many
# person people: id; name; whatever-you-need friendships: id; person_id; friend_id
class Person < ActiveRecord::Base
has_many :friendships, :foreign_key => "person_id",
:class_name => "Friendship"
has_many :friends, :through => :friendships
end
class Friendship < ActiveRecord::Base
belongs_to :person, :foreign_key => "person_id", :class_name => "Person"
belongs_to :friend, :foreign_key => "friend_id", :class_name => "Person"
end
조금 다른 버전.
ActiveRecord::Schema.define(:version => 20100410040453) do
create_table "people", :force => true do |t|
t.string "name"
t.string "sex"
t.integer "father_id"
t.integer "mother_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
# source , belongs_to
class Person < ActiveRecord::Base
self.inheritance_column = "sex"
belongs_to :father, :class_name => "Person"
belongs_to :mother, :class_name => "Person"
end
class Male < Person
has_many :children, :class_name => "Person", :foreign_key => :father_id
has_many :babymommas, :through => :children, :source => :mother, :uniq => true
end
class Female < Person
has_many :children, :class_name => "Person", :foreign_key => :mother_id
has_many :babydaddies, :through => :children, :source => :father, :uniq => true
end
자신의referential 그리고 두 개의 이름, 예를 들면tiwtter 당신의follower,friend
create_table :users do |t|
t.column :login, :string, :null => false
end
create_table :relationships, :id => false do |t|
t.column "user_id", :integer, :null => false
t.column "buddy_id", :integer, :null => false
end
Spec 테스트 섹션
describe User do
before :each do
%w(u v w).each do |x|
eval "@#{x} = User.new; @#{x}.login = '#{x}'; @#{x}.save"
end
end
it "can be linked with another user" do
@u.linked_to << @v
@u.linked_to.should include(@v)
u2 = User.find_by_id @u.id
u2.linked_to.should include(@v)
end
it "can find users that linked to it" do
@u.linked_to << @v
@v.linked_from.should include(@u)
end
it "can see friends of its friends" do
@u.linked_to << @v
@v.linked_to << @w
@u.linked_to[0].linked_to.should include(@w)
@w.linked_from[0].linked_from.should include(@u)
end
it "has a list of all users linked to and from it" do
@u.linked_to << @v
@v.linked_to << @w
@v.linked.should include(@u)
@v.linked.should include(@w)
end
end
모델 설정
class Relationship < ActiveRecord::Base
belongs_to :user,
:class_name => 'User', :foreign_key => 'user_id'
belongs_to :buddy,
:class_name => 'User', :foreign_key => 'buddy_id'
end
class User < ActiveRecord::Base
has_many :relations_to,
:foreign_key => 'user_id', :class_name => 'Relationship'
has_many :relations_from,
:foreign_key => 'buddy_id', :class_name => 'Relationship'
has_many :linked_to,
:through => :relations_to, :source => :buddy
has_many :linked_from,
:through => :relations_from, :source => :user
end
select user_id, buddy_id
from relationships
union
select buddy_id, user_id
from relationships
def linked
User.find_by_sql "
select *
from users
where id in (
select buddy_id
from buddies
where user_id = #{id})"
end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Rails】일대일대다의 어소시에이션결국은 기본적인 것입니다만, 조금 바뀌면 곧바로 빠져 버린다. 어소시에이션이 잘 되지 않고 시간을 녹여 버렸기 때문에, 그런 과거의 자신과 있을지도 모르는 미래의 누군가를 위해서 남겨 둡니다. User 모델과 Tag...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.