2015-04-26 108 views
2

我無法添加對象來關聯類。 父級用戶與廣告類具有has_many關係。 當我嘗試從廣告控制器訪問用戶的has_many對象「:ads」時,它會返回「undefined method ads」異常。我在下面發佈我的模型和我的控制器代碼。請幫我解決這個問題。將對象添加到一對多關係ROR

用戶模型

class User < ActiveRecord::Base 

    has_many :ads 

    has_secure_password 

    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "50x50#" }, :default_url => "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

    def self.searchID(query) 
     where("id like ?", "#{query}") 
    end 
end 

廣告模式

class Ad < ActiveRecord::Base 

    belongs_to :user 

    scope :orderNewestFirst , lambda { order("id DESC") } 

    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

    def self.search(query) 
     where("title like ?", "%#{query}%") 
    end 

end 

廣告控制器

class AdController < ApplicationController 

    layout false 
    before_action :confirm_logged_in 

    def index 
     @ads = Ad.orderNewestFirst 
    end 

    def new 
     @ad = Ad.new() 
    end 

    def create 
     @find = session[:user_id] 
     @user = User.searchID(@find) 

     @ad = Ad.new(ad_params) 

     if @user.ads << @ad #***this is where the error is occuring*** 
      flash[:notice] = "Ad created Successfully" 
      redirect_to(:controller => 'user' , :action => 'index') 
     else 
      render('new') 
     end 
    end 

    def show 
     @ad = Ad.find(params[:id]) 
    end 

    def ad_params 
     params.require(:ad).permit(:title, :category, :description , :priceRange, :avatar) 
    end 
end 

編輯

這裏AR Ë我遷移用戶和廣告

用戶遷移

class CreateUsers < ActiveRecord::Migration 

    def up 
     create_table :users do |t| 
      t.string "firstName" , :limit => 50 , :null => false 
      t.string "lastName" , :limit => 50 
      t.string "email" , :null => false 
      t.string "password" , :limit => 30 , :null => false 
      t.integer "rating" , :default => 0 
      t.string "location" , :default => "Lahore" 
      t.timestamps null: false 
     end 
    end 

    def down 
     drop_table :users 
    end 
end 

#user = User.new(:firstName => "" , :lastName => "" , :email => "" , :password => "" , :rating => "" , :location => "") 

廣告遷移

class CreateAds < ActiveRecord::Migration 
    def up 
     create_table :ads do |t| 
     t.references :user 
      t.string "title" , :null => false 
      t.string "category" , :null => false 
      t.text "description" 
      t.string "priceRange" , :null => false 
     t.attachment :avatar 
     t.timestamps null: false 
     end 
     add_index("ads" , "user_id") 
    end 

    def down 
     drop_table :ads 
    end 

end 


#ad = Ad.new(:title => "" , :category => "" , :description => "" , :priceRange => "") 

,我得到的錯誤..

enter image description here

+0

'ad'模型的表名是什麼?'ad'模型中有'user_id'列嗎? –

+0

請發佈您的完整錯誤stacktrace。 – Pavan

+0

@PramodShinde,我使用引用而不是user_id。 –

回答

3

User.searchID(query)返回多個用戶記錄(一個關係對象),而不是一個單一的之一。

如果將其更改爲User.searchID(query).take,它將僅提取一條記錄,並且錯誤消失。

我不知道什麼值session[:user_id]成立,但看着你的users表,這只是一個整數ID?在這種情況下,目前的方法沒有多少意義。我建議是這樣的:

def create 
    @user = User.find(session[:user_id]) 

    # Rest of create method 
end 

User.find方法查找與精確的ID的用戶記錄,並會返回一個打抱不平了。這是使用Ruby on Rails獲取記錄的最常用方法之一。

+0

它的工作!非常感謝! –

0

我不知道怎麼樣用id查詢是爲你工作,Postgres裏它不

def self.searchID(query) 
    where("id like ?", "#{query}") 
end 

你不需要上面的方法,你可以通過ID作爲

@user = User.find(session[:user_id]) 
直接找到用戶