我怎样才能访问另一个模型 class 属性
How can I get access to another model class attributes
我有两个模型 class,分别是 order.rb 和 customer.rb:
order.rb
class Order < ActiveRecord::Base
belongs_to :customer
validates :customer_id, :name, :age, :presence => true
def self.to_csv
attributes = %w{ to_param name age }
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |t|
csv << attributes.map{ |attr| t.send(attr) }
end
end
end
customer.rb
class Customer < ActiveRecord::Base
belongs_to :order, primary_key: "customer_id"
has_many :orders
validates :phone_number, :name,:email,:presence => true, allow_blank: true
我的问题是如何获取 customer.rb
数据,例如它的电子邮件和姓名属性。然后将其添加到 order.rb
数据中。如果您查看 order.rb
模型,我可以获得它列出的属性:姓名和年龄,但我正在尝试获取 customer.rb
属性,例如电子邮件、姓名和 phone_number。
但是,只有当我应用下面显示的方法并一遍又一遍地打印出相同的电子邮件时,我才能访问一封电子邮件。如果有人可以帮助我,请提前致谢。
def to_param
Customer.new.email
Customer.all.first.email
end
这将 return 个电子邮件 ID 一个接一个地发送 -
Customer.all.each do |customer|
customer.email
end
class Order < ActiveRecord::Base
belongs_to :customer
def self.to_csv
attributes = %w{ phone_number name age }
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |t|
# Note: Considering the attributes are defined in `Customer` model.
# It will get the `customer` of every order and send the message like
# `email`, `name` and maps the responses to the messages
csv << attributes.map { |attr| t.customer.send(attr) }
end
end
end
end
class Customer < ActiveRecord::Base
has_many :orders
validates :phone_number, :name, :email, :presence => true, allow_blank: true
...
end
如果 Order
模型可能无法提供所有属性,那么您可以将缺少的属性委托给 Customer
模型。
# in order.rb
deligate :name, :email, :phone_number, to: :customer, allow_nil: true
# Then this will work; no need of `.customer` as message will be delegated
csv << attributes.map { |attr| t.send(attr) }
:allow_nil
- 如果设置为 true,将阻止引发 NoMethodError
。 See this for more info about delegation.
如果需要进一步的帮助,请在此处发表评论。
我有两个模型 class,分别是 order.rb 和 customer.rb:
order.rb
class Order < ActiveRecord::Base
belongs_to :customer
validates :customer_id, :name, :age, :presence => true
def self.to_csv
attributes = %w{ to_param name age }
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |t|
csv << attributes.map{ |attr| t.send(attr) }
end
end
end
customer.rb
class Customer < ActiveRecord::Base
belongs_to :order, primary_key: "customer_id"
has_many :orders
validates :phone_number, :name,:email,:presence => true, allow_blank: true
我的问题是如何获取 customer.rb
数据,例如它的电子邮件和姓名属性。然后将其添加到 order.rb
数据中。如果您查看 order.rb
模型,我可以获得它列出的属性:姓名和年龄,但我正在尝试获取 customer.rb
属性,例如电子邮件、姓名和 phone_number。
但是,只有当我应用下面显示的方法并一遍又一遍地打印出相同的电子邮件时,我才能访问一封电子邮件。如果有人可以帮助我,请提前致谢。
def to_param
Customer.new.email
Customer.all.first.email
end
这将 return 个电子邮件 ID 一个接一个地发送 -
Customer.all.each do |customer|
customer.email
end
class Order < ActiveRecord::Base
belongs_to :customer
def self.to_csv
attributes = %w{ phone_number name age }
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |t|
# Note: Considering the attributes are defined in `Customer` model.
# It will get the `customer` of every order and send the message like
# `email`, `name` and maps the responses to the messages
csv << attributes.map { |attr| t.customer.send(attr) }
end
end
end
end
class Customer < ActiveRecord::Base
has_many :orders
validates :phone_number, :name, :email, :presence => true, allow_blank: true
...
end
如果 Order
模型可能无法提供所有属性,那么您可以将缺少的属性委托给 Customer
模型。
# in order.rb
deligate :name, :email, :phone_number, to: :customer, allow_nil: true
# Then this will work; no need of `.customer` as message will be delegated
csv << attributes.map { |attr| t.send(attr) }
:allow_nil
- 如果设置为 true,将阻止引发 NoMethodError
。 See this for more info about delegation.
如果需要进一步的帮助,请在此处发表评论。