[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can I save relationship using hasmanythrough

jack.tang

4/6/2007 2:53:00 AM

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?

3 Answers

Joel VanderWerf

4/6/2007 3:55:00 AM

0

jack.tang wrote:
> 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?
>

To save your relationship you might try

(a) flowers

(b) therapy

(c) the rails list

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

jack.tang

4/6/2007 8:01:00 AM

0

On Apr 6, 10:52 am, "jack.tang" <him...@gmail.com> wrote:
> 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?

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

Rick DeNatale

4/7/2007 4:27:00 PM

0

On 4/5/07, jack.tang <himars@gmail.com> wrote:
> 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

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:
http://www.rubyonrails.org...

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...