activerecord

Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find id=

坚强是说给别人听的谎言 提交于 2019-12-12 03:03:53
问题 I'm fairly new to rails and into coding my first app. Just can't figure out how to target a user the current_user favorited (Three models: User, Tool, FavoriteUser). Controller (Tools) def index @favorites = current_user.favorites.order("created_at DESC") @userfavorites = current_user.userfavorites.order("created_at DESC") @tools = Tool.where(user_id: current_user).order("created_at DESC") @user = current_user.favorite_user # problematic! @cuser = current_user end Index View (Tools) %h2 My

undefined method `alias_method_chain' in active record 3.2.18 without rails migration

孤者浪人 提交于 2019-12-12 03:03:21
问题 This is my Rakefile require 'bundler' Bundler.setup require 'active_record' require 'sqlite3' require 'yaml' require 'logger' task :migrate => :environment do ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil ) end task :environment do ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yaml'))['development']) ActiveRecord::Base.logger = Logger.new(STDOUT) end When I execute the task I got this error: rake aborted! /usr/local/rvm

Query for has_many through rails

帅比萌擦擦* 提交于 2019-12-12 02:54:23
问题 Association is class Campaign has_many :views_logs has_many :users, through: :views_logs end I want to get all users in a Campaign within any specific dates etc meaning, I want to do something like: Campaign.first.users.where(?) Now, when I query: Campaign.first.users.all I get list of all users within a certain campaign, but how I can I get only those users which have Campaign between specific date ranges only. 回答1: I want to get all users in a Campaign within any specific dates ActiveRecord

How to search through nested values of attributes in rails

一曲冷凌霜 提交于 2019-12-12 02:53:15
问题 Hi I have a sample app which has 3 objects in seed file: students =[{ name: "John", hobby: "sport"}, { name: "Bill", hobby: "science"}, { name: "Evit Tom", hobby: ["sport","science"]}] students.each do |student| School.create(student) end I search them with check boxes form: <%= form_tag student_list_path %> Hobby: <ul> <li><%= check_box_tag 'hobby[]', "Sport" %> Sport</li> <li><%= check_box_tag 'hobby[]', "Science" %> Science</li> </ul> <%= submit_tag "Show" %> And in controller I have:

Returning issue in Comment::ActiveRecord_Associations_Collection

喜欢而已 提交于 2019-12-12 02:49:40
问题 I'm trying to display all the comments made from a user in a User profile page (no matter if it's a public or a page behind the login), but I'm experiencing an issue that prints in my app Comment::ActiveRecord_Associations_CollectionProxy:0x00000103c89750 (it's not an error that kills my app, just prints that message). the only element commentable within my app are 'hacks', and users can create comments on each hack. user.rb class User < ActiveRecord::Base TEMP_EMAIL_PREFIX = 'change@me' TEMP

has_many :through rails

折月煮酒 提交于 2019-12-12 02:47:09
问题 I want to get user1 to accept requests from other users to join user1s post as contributors and be listed out if user1 accepts them Can't seem to get it to work - Here's my setup: has_many :through I'm thinking: post model class Post < ActiveRecord::Base #Join table associations has_many :group_requests, class_name: "Group", foreign_key: "requester_id", dependent: :destroy has_many :group_users, class_name: "Group", foreign_key: "accepted_id", dependent: :destroy has_many :requester, through:

belongs_to with :conditions in Rails 4

♀尐吖头ヾ 提交于 2019-12-12 02:46:05
问题 I have two models, Users and Accounts: class User < ActiveRecord::Base belongs_to :account, :conditions=>proc{" company_account = #{self.company_user} "} end class Account < ActiveRecord::Base has_many :users end In Rails 3 belongs_to and :conditions=> works fine, but in Rails 4 I read this options is not valid. I tried belongs_to :account, -> {where company_account: self.company_user} but I get error undefined method company_user How can I solve this in Rails 4? 回答1: Try something like:

polymorphic association and setting default value for an asset

匆匆过客 提交于 2019-12-12 02:37:54
问题 Newbie question. I have the following models: class Asset < ActiveRecord::Base belongs_to :assetable, :polymorphic => true #paperclip has_attached_file :asset, :hash_secret => "my-secret", :url => "/images/:hash_:basename_:style.:extension", :path => UPLOAD_PATH + "/:hash_:basename_:style.:extension", :styles => { :medium => "300x300>", :thumb => "75x75>" } end class Location < ActiveRecord::Base has_many :assets, :as => :assetable, :dependent => :destroy end class MenuItem < ActiveRecord:

Order by foreign key in activerecord: without a join?

假如想象 提交于 2019-12-12 02:29:19
问题 I want to expand this question. order by foreign key in activerecord I'm trying to order a set of records based on a value in a really large table. When I use join, it brings all the "other" records data into the objects.. As join should.. #table users 30+ columns #table bids 5 columns record = Bid.find(:all,:joins=>:users, :order=>'users.ranking DESC' ).first Now record holds 35 fields.. Is there a way to do this without the join? Here's my thinking.. With the join I get this query SELECT *

Find all students not enrolled in a class (rails)

﹥>﹥吖頭↗ 提交于 2019-12-12 02:24:48
问题 Been trying to figure this one out for a while and I'm really stumped. So I have a danceclass model class Danceclass < ActiveRecord::Base has_many :danceclass_students has_many :students, through: :danceclass_students I Have a student model: class Student < ActiveRecord::Base has_many :danceclass_students has_many :danceclasses, through: :danceclass_students I have my join model: class DanceclassStudent < ActiveRecord::Base belongs_to :student belongs_to :danceclass I use @danceclass.students