Can I save relationship using hasmanythrough

Hi

In the classic hasmanythrough example:

class Group < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end

class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :through => :memberships
end

group1 = Group.new(…)
group1.users = [User.find_by_id(2)]
group1.save!

it throws the exception: “users=” is one undefined method. So how can
i save the relationship in hasmanythrough?

jack.tang wrote:

    has_many :memberships
    has_many :groups, :through => :memberships

end

group1 = Group.new(…)
group1.users = [User.find_by_id(2)]
group1.save!

it throws the exception: “users=” is one undefined method. So how can
i save the relationship in hasmanythrough?

To save your relationship you might try

(a) flowers

(b) therapy

© the rails list

On Apr 6, 10:52 am, “jack.tang” [email protected] wrote:

    has_many :memberships
    has_many :groups, :through => :memberships

end

group1 = Group.new(…)
group1.users = [User.find_by_id(2)]
group1.save!

it throws the exception: “users=” is one undefined method. So how can
i save the relationship in hasmanythrough?

Hi Guys.

here is another question – how can I load asscocitated class while
fetching. say
User.find_by_name(“test”).groups

The error is “NameError: uninitialized constant User::Groups” , my
rails version is 1.2.3.
Can you tell me how to enable :through function?

/Jack

On 4/5/07, jack.tang [email protected] wrote:

    has_many :memberships
    has_many :groups, :through => :memberships

end

Where’s your Membership model? I think you need something like:

class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
end

group1 = Group.new(…)
group1.users = [User.find_by_id(2)]
group1.save!

it throws the exception: “users=” is one undefined method. So how can
i save the relationship in hasmanythrough?

This has nothing to do with the :through option. The :has_many
relationship doesn’t add a setter. Instead you need to use methods on
the attribute, which is not a normal ruby array but an ActiveRecord
object which looks like an Array but is doing the right SQL magic
under the covers.

Some useful methods include:
group1.users<< user # add a user
group1.users.push(user1, user2, …) add one or more users
group1.users.replace(user1, …) replace users with one or more
users
group1.users.delete(user1, …) delete one or more users.

If you want to create a :has_many:through: relationship using just the
two ends, both far ends of the association need to exist on the
database:

group1 = Group.create(…) # group needs to have been previously saved
# … represents
attributes to be set in the group
group1.users << User.find_by_id(2)

Or to create a new user and add it to the group:

group1 = Group.create(…)
article.users.create!(…)

If you want to have attributes in the membership if you don’t why are
you using has_many:through:? Then you need to create the
intermediate table yourself. So lets’ say the membership is used to
hold things like when it expires:

membership = Membership.new
membership.expiration = Time.now.next_year.to_date
membership.group = Group.find_by_id(3) # or how ever you get the group
membership.user = User.find_by_username(“Dirk Gently”)
membership.save

You’ll probably get more help with ActiveRecord questions on more
rails specific resources:


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/