Has_many AND has_many :through?

Hi,
I am working on a scheduling app and I have a perpelextion (new word).

I am wondering if the problem is my data model

I have Users.
Users can create Events.
Users can be invited to Events created by other Users.
So…

user.rb
class User < ActiveRecord::Base
has_many :invitations # invitations to other users’ events
has_many :events, :through => :invitations # all events the user is
invited to

#HERE IS THE PROBLEM
has_many :events # the events that the user is the owner of
end

event.rb
class Event < ActiveRecord::Base
has_many :invitations
belongs_to :user
end

invitation.rb
class Invitation < ActiveRecord::Base
belongs_to :user
belongs_to :event
end

I am not sure what is actually happening but my console tests are not
going very well. Maybe i should just go to bed but…

On 7/22/06, Dinshaw G. [email protected] wrote:

user.rb
class Event < ActiveRecord::Base
I am not sure what is actually happening but my console tests are not
going very well.

You are using has_many :events twice.

Try changing

has_many :events, :through => :invitations

to

has_many :invited_events, :through => :invitations

or something so the two are not the same.

Peter

How abou:

class Invitation < ActiveRecord::Base
belongs_to :invited_user, :class_name => ‘User’
belongs_to :event
end

(and invited_user_id as the table column name)

class Event < ActiveRecord::Base
has_many :invitations
belongs_to :created_by, :class_name => ‘User’
end
(and created_by as the table column name)

Now, in User:
class User < ActiveRecord::Base
has_many :invitations # invitations to other users’ events
has_many :created_events, :foreign_key => ‘created_by’
has_many :invited_events, :through => :invitations, :foreign_key =>
‘invited_user_id’
end

I think this should work :slight_smile:

thank you Dr. Nic for the late night consultation.
havn’t finished yet but it looks like that is working great so far!

Dr Nic wrote:

How abou:

class Invitation < ActiveRecord::Base
belongs_to :invited_user, :class_name => ‘User’
belongs_to :event
end

(and invited_user_id as the table column name)

class Event < ActiveRecord::Base
has_many :invitations
belongs_to :created_by, :class_name => ‘User’
end
(and created_by as the table column name)

Now, in User:
class User < ActiveRecord::Base
has_many :invitations # invitations to other users’ events
has_many :created_events, :foreign_key => ‘created_by’
has_many :invited_events, :through => :invitations, :foreign_key =>
‘invited_user_id’
end

I think this should work :slight_smile:

class User < ActiveRecord::Base
has_many :invitations_received, :class => ‘Invitation’, :foreign_key
=> ‘to_user’’
has_many :invited_events, :through => :invitations_received, :source =>
‘event’
has_many :events
end

class Event < ActiveRecord::Base
belongs_to :user
end

class Invitation < ActiveRecord::Base
belongs_to :who, :class => ‘User’, :foreign_key => ‘from_user’
belongs_to :whom, :class => ‘User’, :foreign_key => ‘to_user’
belongs_to :event
end

You can also create more relations using :who, :whom.

On 7/23/06, Dinshaw G. [email protected] wrote:

user.rb
class Event < ActiveRecord::Base
I am not sure what is actually happening but my console tests are not
going very well. Maybe i should just go to bed but…


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


rm -rf / 2>/dev/null - http://null.in

“Things do not happen. Things are made to happen.” - JFK

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs