rails 4有很多通过关联查询
rails 4 has many through association query
我有这些模型,但是当我执行 Message.last.people 或 Message.last.recipient_lists 时出现错误。我如何引用收件人列表或附加到具有活动记录的邮件的人员?我需要做一个 RecipientList.where(:message => Message.last)
吗?似乎应该有更好的方法通过 .message 来做到这一点?
class Message < ActiveRecord::Base
has_many :people, through: :recipient_list
end
class Person < ActiveRecord::Base
has_many :messages, through: :recipient_list
end
class RecipientList < ActiveRecord::Base
belongs_to :person
belongs_to :message
end
我收到这个错误
Message.last.recipient_lists
Message Load (0.7ms) SELECT
"messages".* FROM "messages" ORDER BY "messages"."id" DESC LIMIT 1
NoMethodError: undefined method `recipient_lists' for
from /home/nick/.rvm/gems/ruby-2.0.0-p598/gems/activemodel-4.0.9/lib/active_model/attribute_methods.rb:439:in
method_missing' from
/home/nick/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.9/lib/active_record/attribute_methods.rb:168:in
method_missing' from (irb):1 from
/home/nick/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.9/lib/rails/commands/console.rb:90:in
start' from
/home/nick/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.9/lib/rails/commands/console.rb:9:in
start' from
/home/nick/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.9/lib/rails/commands.rb:62:in
<top (required)>' from bin/rails:4:in
require' from bin/rails:4:in
`'
Message.last.people Message Load (1.0ms) SELECT "messages".* FROM
"messages" ORDER BY "messages"."id" DESC LIMIT 1
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find
the association :recipient_list in model Message
您需要在 Message 和 Person 中明确包含 has_many :recipient_lists
,并在 through:
选项中正确复数 :recipient_lists
,如下所示:
class Message < ActiveRecord::Base
has_many :recipient_lists
has_many :people, through: :recipient_lists
end
class Person < ActiveRecord::Base
has_many :recipient_lists
has_many :messages, through: :recipient_lists
end
class RecipientList < ActiveRecord::Base
belongs_to :person
belongs_to :message
end
我有这些模型,但是当我执行 Message.last.people 或 Message.last.recipient_lists 时出现错误。我如何引用收件人列表或附加到具有活动记录的邮件的人员?我需要做一个 RecipientList.where(:message => Message.last)
吗?似乎应该有更好的方法通过 .message 来做到这一点?
class Message < ActiveRecord::Base
has_many :people, through: :recipient_list
end
class Person < ActiveRecord::Base
has_many :messages, through: :recipient_list
end
class RecipientList < ActiveRecord::Base
belongs_to :person
belongs_to :message
end
我收到这个错误
Message.last.recipient_lists
Message Load (0.7ms) SELECT "messages".* FROM "messages" ORDER BY "messages"."id" DESC LIMIT 1 NoMethodError: undefined method `recipient_lists' forfrom /home/nick/.rvm/gems/ruby-2.0.0-p598/gems/activemodel-4.0.9/lib/active_model/attribute_methods.rb:439:in
method_missing' from /home/nick/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.0.9/lib/active_record/attribute_methods.rb:168:in
method_missing' from (irb):1 from /home/nick/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.9/lib/rails/commands/console.rb:90:instart' from /home/nick/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.9/lib/rails/commands/console.rb:9:in
start' from /home/nick/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.0.9/lib/rails/commands.rb:62:in<top (required)>' from bin/rails:4:in
require' from bin/rails:4:in `'Message.last.people Message Load (1.0ms) SELECT "messages".* FROM "messages" ORDER BY "messages"."id" DESC LIMIT 1 ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :recipient_list in model Message
您需要在 Message 和 Person 中明确包含 has_many :recipient_lists
,并在 through:
选项中正确复数 :recipient_lists
,如下所示:
class Message < ActiveRecord::Base
has_many :recipient_lists
has_many :people, through: :recipient_lists
end
class Person < ActiveRecord::Base
has_many :recipient_lists
has_many :messages, through: :recipient_lists
end
class RecipientList < ActiveRecord::Base
belongs_to :person
belongs_to :message
end