'2009/04'에 해당되는 글 3건
-
2009/04/15
-
2009/04/14
-
2009/04/07
제목을 뭐로 해야할지 참 애매하네요.
지금 만들려고 하는 Join Table은, 같은 Model을 향하는 Join Table입니다.(말이 어렵네요. ㅎㅎ)
Subscription 이라는 Join Table은 user_id, writer_id를 가지고 있습니다.
user_id, writer_id 모두 User Model을 가르키는 ID를 가지고 있죠.
제가 하고 싶은것은
User.subscriptions # User가 구독하고 있는 모든 Users
User.subscribers # User를 구독하고 있는 모든 Users
입니다.
Join Table명을 (딱히 좋은 이름이 생각안나서) Subscript로 변경하겠습니다.
코드 설명을 세세하게 하면 싫어하실것 같아 간략하게 코드 설명을 하겠습니다.
Subscript의 writer_id를 이용하는 새로운 관계(belongs_to)를 만듭니다.
그리고 그 만든 관계명을 User에서 writer_id로 User를 불러와야 하는 곳(:subscriptions)의 :source로 써준 것이죠.
좀 찜찜하지만 이런식으로도 구현할 수 있습니다.
하지만.. 그래도 역시 찜찜하죠? ㅎㅎ
여담>
지금 딱 새벽 6시 20분이네요. 오랜만에 와이프 아침밥이나 해주고 자야겠네요 ㅎㅎ (라고 말하고 기절하기)
지금 만들려고 하는 Join Table은, 같은 Model을 향하는 Join Table입니다.(말이 어렵네요. ㅎㅎ)
Subscription 이라는 Join Table은 user_id, writer_id를 가지고 있습니다.
user_id, writer_id 모두 User Model을 가르키는 ID를 가지고 있죠.
제가 하고 싶은것은
User.subscriptions # User가 구독하고 있는 모든 Users
User.subscribers # User를 구독하고 있는 모든 Users
입니다.
Join Table명을 (딱히 좋은 이름이 생각안나서) Subscript로 변경하겠습니다.
class Subscript < ActiveRecord::Base
belongs_to :user
belongs_to :writer, :foreign_key => "writer_id", :class_name => "User"
end
belongs_to :user
belongs_to :writer, :foreign_key => "writer_id", :class_name => "User"
end
class User < ActiveRecord::Base
has_many :subscripts #내(Logged User)가 구독하고 있는 User ids
has_many :subscriptions, :through => :subscripts, :source => :writer #내가 구독하는 Users
has_many :subscribes, :foreign_key => "writer_id", :class_name => "Subscript" #날 구독하는 User ids
has_many :subscribers, :through => :subscribes, :source => :user #날 구독하는 Users
end
has_many :subscripts #내(Logged User)가 구독하고 있는 User ids
has_many :subscriptions, :through => :subscripts, :source => :writer #내가 구독하는 Users
has_many :subscribes, :foreign_key => "writer_id", :class_name => "Subscript" #날 구독하는 User ids
has_many :subscribers, :through => :subscribes, :source => :user #날 구독하는 Users
end
코드 설명을 세세하게 하면 싫어하실것 같아 간략하게 코드 설명을 하겠습니다.
Subscript의 writer_id를 이용하는 새로운 관계(belongs_to)를 만듭니다.
그리고 그 만든 관계명을 User에서 writer_id로 User를 불러와야 하는 곳(:subscriptions)의 :source로 써준 것이죠.
좀 찜찜하지만 이런식으로도 구현할 수 있습니다.
class User < ActiveRecord::Base
has_many :subscripts, :dependent => :destroy
def subscriptions
ids = self.subscripts.collect { |s| s.writer_id }
User.find(ids)
end
end
확장성의 제약이 있겠지만, 사용하는것에는 큰 문제점이 없을듯 하네요has_many :subscripts, :dependent => :destroy
def subscriptions
ids = self.subscripts.collect { |s| s.writer_id }
User.find(ids)
end
end
하지만.. 그래도 역시 찜찜하죠? ㅎㅎ
여담>
지금 딱 새벽 6시 20분이네요. 오랜만에 와이프 아침밥이나 해주고 자야겠네요 ㅎㅎ (라고 말하고 기절하기)
'작은 팁' 카테고리의 다른 글
| [Rails] mac에서 rails하는 사람들이 snow leopard 설치시 주의할 점 (2) | 2009/09/14 |
|---|---|
| [Rails] 같은 Model을 has_many :through하기 (0) | 2009/04/15 |
| [Rails] 2.3 Test 에서 undefined method `use_transactional_fixtures=' 나올때. (0) | 2009/04/14 |
| [Rails] 2.2.2 로 개발하던 것을 2.3.2 로 바꿀때. (0) | 2009/04/07 |
| gem으로 mime-types update 혹은 install 시 나는 Error(on Gentoo) (2) | 2009/03/10 |
| IE7에서 text-decoration이 이상하게 나올때. (0) | 2009/02/02 |
Trackback 0 And
Comment 0
test/test_helper.rb 에서
class Test::Unit::TestCase
를
class ActiveSupport::TestCase
요렇게 바꿔주면 됩니다.
참고링크
'작은 팁' 카테고리의 다른 글
| [Rails] mac에서 rails하는 사람들이 snow leopard 설치시 주의할 점 (2) | 2009/09/14 |
|---|---|
| [Rails] 같은 Model을 has_many :through하기 (0) | 2009/04/15 |
| [Rails] 2.3 Test 에서 undefined method `use_transactional_fixtures=' 나올때. (0) | 2009/04/14 |
| [Rails] 2.2.2 로 개발하던 것을 2.3.2 로 바꿀때. (0) | 2009/04/07 |
| gem으로 mime-types update 혹은 install 시 나는 Error(on Gentoo) (2) | 2009/03/10 |
| IE7에서 text-decoration이 이상하게 나올때. (0) | 2009/02/02 |
Trackback 0 And
Comment 0
environment.rb 에서 RAILS_GEM_VERSION 을 '2.2.2'로 개발중이던것을
'2.3.2'로 수정하면..
uninitialized constant ApplicationController
라는 Error Message가 뜹니다.
Rails가 2.3 으로 Update 하면서 application.rb 가 application_controller.rb 로 변경이 되어서 생기는 문제점입니다.
직접 application.rb 파일을 rename 해주거나.
rake rails:update:application_controller
이런식으로 rake를 해주셔도 됩니다.
저는 rake를 사용해서 작업했습니다.
이제 잘 되네요 :)
관련링크
덧.
위에 적은 rake는 rake rails:update에 포함되어 있다고 하네요.
'작은 팁' 카테고리의 다른 글
| [Rails] 같은 Model을 has_many :through하기 (0) | 2009/04/15 |
|---|---|
| [Rails] 2.3 Test 에서 undefined method `use_transactional_fixtures=' 나올때. (0) | 2009/04/14 |
| [Rails] 2.2.2 로 개발하던 것을 2.3.2 로 바꿀때. (0) | 2009/04/07 |
| gem으로 mime-types update 혹은 install 시 나는 Error(on Gentoo) (2) | 2009/03/10 |
| IE7에서 text-decoration이 이상하게 나올때. (0) | 2009/02/02 |
| Conditional comments를 이용해서 CSS-Hack 안쓰고 코딩하기 (4) | 2008/11/27 |
Trackback 0 And
Comment 0
Prev









