简体   繁体   English

Ruby On Rails模型没有方法

[英]Ruby On Rails Model has no methods

I have a model with a belongs to relationship. 我有一个属于关系的模型。

class Product < ActiveRecord::Base
  attr_accessible :name, :price, :request_id, :url

  # Relationships
  belongs_to :request

end

class Request < ActiveRecord::Base
  attr_accessible :category, :keyword

  # Relationships
  has_many :products

end

This is the code in my controller function product = Product.where({ :asin => asin }).first 这是我的控制器函数product = Product.where({:asin => asin})中的代码

     # See if the product exists
     begin
         #This throws a method not found error for where
        product = Product.where({ :name => name }).first

     rescue 
        Product.new
             # This throws a method not found error for request_id
        product.request_id = request.id
        product.save
     end

I'm trying to create a new product object like so product = Product.first(:conditions => { :name => name }) 我正在尝试创建一个新的产品对象,例如product = Product.first(:conditions => {:name => name})

When I call that I get an error saying undefined method 'first' for Product:Class I tried doing Product.new and I can't access any attributes. 当我打电话说我得到一个错误,说明产品的undefined method 'first' for Product:Class我尝试做Product.new而我无法访问任何属性。 I get this for every one undefined method 'request_id=' for #<Product:0x007ffce89aa7f8> undefined method 'request_id=' for #<Product:0x007ffce89aa7f8>每个undefined method 'request_id=' for #<Product:0x007ffce89aa7f8>得到这个

I've been able to save request objects. 我已经能够保存请求对象了。 What am I doing wrong with products? 我的产品出了什么问题?

EDIT: 编辑:

So as it turns out there was an old Product data type that was being imported that wasn't an ActiveRecord class. 事实证明,导入的旧Product数据类型不是ActiveRecord类。 It was using that instead of my Product::ActiveRecord. 它使用的是而不是我的Product :: ActiveRecord。 I deleted that import and it's good to go. 我删除了那个导入,这很好。 Sorry to have wasted everybody's time. 很抱歉浪费了每个人的时间。

Not sure what the proper protocol is here for what to do with this question. 不知道这个问题的正确协议是什么。

Is your Product class an ActiveRecord::Base class? 您的Product类是ActiveRecord :: Base类吗? You can find out by running: 您可以通过运行找到:

Product.ancestors.include?(ActiveRecord::Base)

If this returns false, it's getting the class loaded from somewhere else. 如果返回false,则从其他地方加载类。

First check to see that your Product class is set up correctly by typing in: 首先通过输入以下内容检查您的Product类是否已正确设置:

rails c
# after console has loaded
Product

If this looks correct then we will try to instantiate a product by calling: 如果这看起来正确,那么我们将尝试通过调用实例化产品:

# Create a new product
product = Product.new(name: "first product", price: 100, url: "http://www.example.com")
# Persist this object to the database
product.save

If you are missing any attributes run another migration to add them to the Product table. 如果缺少任何属性,请运行另一个迁移以将其添加到Product表中。

If none of those suggestions work, check to make sure that there isn't an existing class with the same name in your project. 如果这些建议都不起作用,请检查以确保项目中没有现有的具有相同名称的类。 This would cause all kinds of errors and would explain certain methods not being found. 这会导致各种错误,并会解释某些未找到的方法。

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

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