Tutorial :has_many :through association



Question:

Is it possible to make some kinda this association:

User model:

has_many :goods, :through => :assignments  has_many :goods_want, :through => :assignments, :source => :good, :conditions => "assignments.type = 1"  

Testing in console

u = User.first  u.goods_want << Good.first  u.save  

This is being logged:

INSERT INTO `assignments` (`good_id`, `updated_at`, `type`, `profile_id`, `created_at`) VALUES(1, '2009-03-26 09:36:11', NULL, 1, '2009-03-26 09:36:11')  

So is there any beautiful way to make this association work not only to get records from database, but to write into database?


Solution:1

Why don't you create an Assignment?

a = Assignment.new  a.type = 1  a.good = Good.first  a.user = User.first  a.save  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »