I want the current user to read and buy postings in clubs in which he is a member. I would like to do something like the following, but I haven’t clicked anything that I read or didn’t try. I will use many nested collections of this type, I'm stuck!
Models
class User < ActiveRecord::Base
has_many :memberships
has_many :clubs, :through => :memberships
has_many :products
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :club
class Club < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
has_many :events
class Event < ActiveRecord::Base
belongs_to :club
class Product < ActiveRecord::Base
belongs_to :user
has_many :posts
class Post < ActiveRecord::Base
belongs_to :product
belongs_to :event
Message controller
def index
@user = current_user
@posts_in_my_clubs = @user.memberships.collect { |a| a.products.posts}
posts /index.html.erb
@posts_in_my_clubs do |post|
Gives me an error:
undefined `memberships' method for zero: NilClass
To which I am not surprised. In addition, I want to be able to select only those messages that will only be in a certain event, etc.
: . , , . , , .
, , , ActiveRecord. Association Join Models . , .
, , . , , ,
def member?(club)
return !!self.clubs.find(club)
, , , - . ? .. , , ...
Logger.info
@user = current_user
logger.info @user
logger.info 'super secret message'
#<User:0x5b202a0>
super secret message
, , "" (!). , , , .. tho