简体   繁体   English

从控制器导轨之外的模型获取数据

[英]getting data from a model outside of controller rails

I need to get the profile of a selected order (Print order). 我需要获取所选订单(打印订单)的资料。

My profiles table has a user_id column and my prints table also has a user_id column. 我的profiles表有一个user_id列,而我的prints表也​​有一个user_id列。

The Profile and Print model belongs_to :user model. ProfilePrint模型belongs_to :user模型。

Print model: 打印模型:

class Print < ActiveRecord::Base
  attr_accessible :comment, :document
  belongs_to :user
end

and, Profile model: 并且,配置文件模型:

class Profile < ActiveRecord::Base
  attr_accessible :address, :name, :phone
  belongs_to :user
  has_many :prints
end

How do I get the data from the profile for prints according to the user? 如何根据用户从打印配置文件中获取数据?

suppose you have print object & user has one profile then 假设您有print对象,并且user has one profile

print.user.profile

Also Profile has_many :prints which is redundant you can directly used 还有Profile has_many :prints这是多余的,您可以直接使用

print.profile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM