Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 数据库查询-Rails_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails 数据库查询-Rails

Ruby on rails 数据库查询-Rails,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我正试图编写一个数据库查询,但我一直在为自己做错了什么而挠头 这是相关的模型 class User has_one :store has_many :products, through: :store enum gender: %i[menswear womenswear unisex] def menswear self.gender == 'menswear' end def womenswear self.gender == 'womens

我正试图编写一个数据库查询,但我一直在为自己做错了什么而挠头

这是相关的模型

class User

  has_one :store
  has_many :products, through: :store

  enum gender: %i[menswear womenswear unisex] 

  def menswear
    self.gender == 'menswear'
  end

  def womenswear
    self.gender == 'womenswear'
  end

end

class Product
  belongs_to :store
end
控制器是

class UsersController

  def index
    @male = User.menswear
    @female = User.womenswear
    @products = Product.all.order('created_at DESC')
  end

end
看法

更新:我将用户控制器更改为

def index
  @partners = User.partner
  @male = User.find_by(gender: 0)
  @female = User.find_by(gender: 1)
end

它可以工作,但只返回男性和女性的第一个实例。我需要它返回它的全部

您正在混合
有一个:通过
关联
有很多:通过
关联。您可以检查适当的文档。您正在使用
has_one:store
并试图通过::store
关联
has_many:products,这是不可能的

要么让它有一个通过像:

has_one :store
has_one :product, through: :store
has_many :stores
has_many :product, through: :stores
或者针对您的情况,让它有很多:通过像:

has_one :store
has_one :product, through: :store
has_many :stores
has_many :product, through: :stores
或者您可以从
has\u many:products中删除through,通过::store
并使其成为
has\u many:products

选择解决方案符合您的要求。

尝试更换:

def index
  @partners = User.partner
  @male = User.find_by(gender: 0)
  @female = User.find_by(gender: 1)
end


您尝试调用用户关系上的
User\products
实例方法。那么它应该是User.store.products@MarekLipkaI可能真的与rails脱节,但它看起来像是
男装
用户
的一种实例方法。但您将其用作类方法。我不知道你是如何在
@male
上调用
产品的,你能给我看一下数据库架构或迁移文件吗。@Thanajaya迁移文件到底是什么?
试图从用户对象检索产品时,
的未定义方法“products”出错。您现在遇到问题了吗?错误是因为在
ActiveRecord\u Relation
上调用
products
。无论您如何定义关联,错误都将保持原样。返回了一个未定义的方法“products”,用于#
 def index
      @partners = User.partner
      @male = User.where(gender: 0)
      @female = User.where(gender: 1)
    end