I have the following model
class Vote include Mongoid::Document include Mongoid::Timestamps field :value, :type => Symbol # can be :aye, :nay, :abstain, :present belongs_to :user belongs_to :polco_group belongs_to :bill
and Bill
class Bill has_many :votes
and a User
class User has_many :votes
I am trying to implement the following test
b = Bill.new @user1.vote_on(b, :aye) assert_equal :aye, b.voted_on?(@user1)
This test is failing, because if I follow those steps, b.votes.all
is empty, but b.votes
has the data we need. However, if I open the rails console, I get that b.votes
is [], but b.votes.all
is fully populated if I follow these steps. I am sure there is something simple I'm missing here. When is b.votes [] and the .all needed?
my methods:
# in User.rb def vote_on(bill, value) # test to make sure the user is a member of a group my_groups = self.joined_groups unless my_groups.empty? unless bill.voted_on?(self) my_groups.each do |g| unless Vote.create(:value => value, :user => self, :polco_group => g, :bill => bill) raise "vote not valid" end end end #bill.save! else raise "no joined_groups for this user" end end
and
# in Bill.rb def voted_on?(user) if votes = self.votes.all.select{|v| v.user == user} votes.map{|v| v.value}.first else nil end end
Bill.new
withBill.create
, probably that should solve most of your problems. If it works let me know, will try to explain the logic. – rubish