Created
August 7, 2012 18:47
-
-
Save sikachu/3288256 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
has_many :topics | |
has_many :posts, through: topics | |
end | |
class Topic | |
belongs_to :user | |
has_many :posts | |
end | |
class Post | |
belongs_to :topic | |
belongs_to :user, deferred_from: topic | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@user = User.find(42) | |
Post.create(user: @user) | |
@post = @topic.posts.create(content: 'foooo') | |
@post.user # => #<User id: 42> | |
@post.user_id # => 42 | |
# Of course, you can override it: | |
@post = @topic.posts.create(user: User.find(24), content: 'foooo') | |
@post.user # => #<User id: 24> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment