ActiveRecord :through to set default values on through table.

Posted by Dmitriy Likhten on Stack Overflow See other posts from Stack Overflow or by Dmitriy Likhten
Published on 2010-05-12T01:12:23Z Indexed on 2010/05/12 1:14 UTC
Read the original article Hit count: 217

Filed under:

I would like to set a default value in a has_many through association.

Lets say I have three models:

People

Friends

Dogs

A person can request that a dog becomes their friend.

So a person would create an association where friends has an active column = false.

User
  has_many :friends
  has_many :dogs, :through => :friends

Now when I assign a dog to a user

User.find(1).dogs << dog

The friends table has null in the active column.

My friends model is defined as

Friend
  def initialize(args = {})
    super(args)
    active = false
  end

yet this does not work because the friend object is never created. Do I have to manually create one?

© Stack Overflow or respective owner

Related posts about activerecord