Rails、嵌套模型、数组、通过“for”迭代环形
我是 Ruby 新手,正在遵循这个 novawave 教程(有点旧)创建内部消息系统。
http://www.novawave.net/public/rails_messaging_tutorial.html
我目前尝试列出已发送的消息,但我无法正确链接到消息“收件人”。这是 for 循环:
<% for message in @messages %>
<tr>
<td><%= message.user.name %></td>
<td><%= message.subject %></td>
<td><%= message.recipients.map(&:user).to_sentence %></td>
我收到“收件人”的“统一常量”错误 我有三个模型:User、Message_Copy 和 Message。
消息模型 属于用户,
has_many :收件人,:通过 => :message_copies
Message_Copy 模型 belongs_to 消息和收件人
用户模型 有许多其他模型。
我在其他应用程序中注意到同样的问题。当我迭代数组时,如果主模型“属于”另一个模型,我可以访问该模型。例如,我可以访问用户模型,因为消息模型“属于”用户模型。因此我可以使用 message.user.name。但我无法访问“属于”主模型的模型的属性。所以我无法访问该数组中的 Message_Copy 模型属性。因此 message.recipient.id 将返回错误。这个问题可能已经有了答案,而我可能只是犯了一系列简单的错误。但我很困惑,希望有人能提供帮助并可能解释我所缺少的内容。如果需要更多信息,请告诉我。谢谢抱歉
,这里是模型的更多代码: 用户模型
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :sent_messages, :class_name => "Message"
has_many :received_messages, :class_name => "MessageCopy", :foreign_key =>"recipient_id"
has_many :folders
class MessageCopy < ActiveRecord::Base
belongs_to :message
belongs_to :recipient
belongs_to :folder
delegate :user, :created_at, :subject, :body, :recipients, :to => :message
end
class Message < ActiveRecord::Base
belongs_to :user
has_many :message_copies
has_many :recipients, :through => :message_copies
before_create :prepare_copies
attr_accessor :to # array of people to send to
attr_accessible :subject, :body, :to
def prepare_copies
return if to.blank?
to.each do |recipient|
recipient = User.find(recipient)
@recipient=recipient
message_copies.build(:recipient_id => recipient.id, :folder_id => recipient.inbox.id)
end
end
end
我注意到这有效: <%= message.message_copies %>
而不是上面的代码:
<td><%= message.recipients %></td>
我不明白这一点,因为教程可以使用“收件人”,也因为我可以通过其他方法中的收件人引用 Message_Copy 模型。
但 <%= message.message_copies %> 也只返回一个数组,我不清楚如何访问 message_copy 模型中的特定属性,因为 <%= message.message_copies.content %> 不起作用。
谢谢你!如果需要的话我会发布更多代码
I'm new to Ruby and following this novawave tutorial (a little older) on creating an internal messaging system.
http://www.novawave.net/public/rails_messaging_tutorial.html
I'm currently attempting to list the sent messages, but I am unable to properly link to the message "recipients." Here is the for loop:
<% for message in @messages %>
<tr>
<td><%= message.user.name %></td>
<td><%= message.subject %></td>
<td><%= message.recipients.map(&:user).to_sentence %></td>
I'm getting an "unitialized constant" error for "recipients" I have three models: User, Message_Copy, and Message.
Message Model
belongs_to User,
has_many :recipients, :through => :message_copies
Message_Copy model
belongs_to Message and recipient
User model
has_many of the other models.
I've noticed this same problem in other applications. When I iterate through the array, if the primary model "belongs to" another model, I can access that model. For example, I can access the User model because the Message model "belongs_to" the User model. Hence I can use message.user.name. But I cannot access the attributes of a model that "belongs_to" the primary model. So I cannot access the Message_Copy model attributes in this array. So message.recipient.id will return an error. This question may have already been answered, and I may just be making a series of easy mistakes. But I'm confused and was hoping someone could help and possibly explain what I'm missing. Please let me know if more informaiton is required. Thank you
Apologies, here is more code from the models:
User Model
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :sent_messages, :class_name => "Message"
has_many :received_messages, :class_name => "MessageCopy", :foreign_key =>"recipient_id"
has_many :folders
class MessageCopy < ActiveRecord::Base
belongs_to :message
belongs_to :recipient
belongs_to :folder
delegate :user, :created_at, :subject, :body, :recipients, :to => :message
end
class Message < ActiveRecord::Base
belongs_to :user
has_many :message_copies
has_many :recipients, :through => :message_copies
before_create :prepare_copies
attr_accessor :to # array of people to send to
attr_accessible :subject, :body, :to
def prepare_copies
return if to.blank?
to.each do |recipient|
recipient = User.find(recipient)
@recipient=recipient
message_copies.build(:recipient_id => recipient.id, :folder_id => recipient.inbox.id)
end
end
end
I noticed this worked:
<%= message.message_copies %>
Instead of the above code:
<td><%= message.recipients %></td>
I don't understand this because the tutorial can use "recipients" and also because I can reference the Message_Copy model through Recipients in other methods.
But
<%= message.message_copies %>
also just returns an array, and I'm unclear how I can reach the specific attributes in the message_copy model, since
<%= message.message_copies.content %>
does not work.
Thank you! I'll post more code if necessary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论