ruby on rails - Adding an object to one to many relationship ROR -
i having trouble adding object associate class. parent class, user, has has_many relation ad class. when try access user's has_many object ":ads" ad controller, returns me "undefined method ads" exception. posting model , controller code below. please me on issue.
user model
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 ?", "#{query}") end end
ad model
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 ?", "%#{query}%") end end
ad controller
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 error 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
edit
here migrations user , ad
user migration
class createusers < activerecord::migration def create_table :users |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 => "")
ad migration
class createads < activerecord::migration def create_table :ads |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 => "")
the error get..
your user.searchid(query)
returns multiple user records (a relation object), not single one.
if change user.searchid(query).take
, fetches single record, , error vanishes.
i don't know value session[:user_id]
holds, looking @ users
table, integer id? in case, there isn't sense in current approach. i'd recommend this:
def create @user = user.find(session[:user_id]) # rest of create method end
the user.find
method looks user record exact id, , return single record straight away. 1 of common ways fetch records using ruby on rails.
Comments
Post a Comment