class Blog < ActiveRecord::Base
has_many :blog_authors, :inverse_of => :blog
has_many :users, :through => :blog_authors
end
class BlogAuthor < ActiveRecord::Base
belongs_to :user
belongs_to :blog
end
class User < ActiveRecord::Base
has_many :blog_authors, :inverse_of => :user
has_many :blogs, :through => :blog_authors
end
[code]user = User.last
User Load (0.2ms) SELECT users
.* FROM users
ORDER BY users
.id
DESC LIMIT 1
=> #<User id: 5, role_id: nil, email: "user@mail.com" … l>
blog = user.blogs.build(:name => “zxc”)
blog.save!
(0.3ms) BEGIN
Blog Exists (0.5ms) SELECT 1 AS one FROM blogs
WHERE blogs
.slug
= BINARY ‘zxcc’ LIMIT 1
SQL (0.4ms) INSERT INTO blogs
(author_id
, created_at
, footer
, name
, updated_at
) VALUES (NULL, ‘2013-03-26 13:30:41’, NULL, ‘zxc’, ‘2013-03-26 13:30:41’)
(3.1ms) COMMIT
=> true
BlogAuthor.all
BlogAuthor Load (0.4ms) SELECT blog_authors
.* FROM blog_authors
=> [][/code]
help