Reputation: 5167
I'm wondering... (1) what should be the correct associations for 2 different tables both with 2 same columns (2) how do i display the user list in views with for loop
So one table is called Attending and has 2 columns: Events & Users Another table is called NotAttending and has 2 columns: Events & Users
class User < ActiveRecord::Base
has_many :attending
has_many :notattending
has_many :events, :through => :attending
has_many :events, :through => :notattending
end
class Event < ActiveRecord::Base
has_many :attending
has_many :notattending
has_many :users, :through => :attending
has_many :users, :through => :notattending
end
class Attending < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
class Notattending < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
How would I display the list of users for attending and notattending in the views? I get error undefined method users for nil:NilClass
<% for user in @attending.user %>
<%= user.name %></br>
<% end %>
Thanks!
Upvotes: 0
Views: 108
Reputation: 44992
ASIDE: why not combine Attending and Nonattending into one table with three columns, event, user, and is_attending (true if attending, false if not attending)?
But no matter, let's assume the data model is fixed...
You cannot use has_many :users twice. You could choose to make another method:
class User < ActiveRecord::Base
has_many :attending
has_many :notattending
def events
self.attending.map(&:events) + self.nonattending.map(&:events)
end
end
class Event < ActiveRecord::Base
has_many :attending
has_many :notattending
def users
self.attending.map(&:users) + self.nonattending.map(&:users)
end
end
Upvotes: 1