activerecord

Rails 4, has_many through association - finding associated objects

穿精又带淫゛_ 提交于 2019-12-25 07:18:52
问题 I'm wondering why Item.where(category: x) isn't working for me. I expect this statement to return all items which are categorized under category x. Please see below my associations: class Category < ActiveRecord::Base has_many :categorizations has_many :items, :through => :categorizations end class Item < ActiveRecord::Base has_many :categorizations has_many :categories, :through => :categorizations end class Categorization < ActiveRecord::Base belongs_to :item belongs_to :category end

where's the appropriate place to handle writing the current_user.id to an object

青春壹個敷衍的年華 提交于 2019-12-25 06:53:03
问题 I am not using Devise but have implemented a simple authentication scheme (basically outlined here http://railscasts.com/episodes/250-authentication-from-scratch) with the relevant part being here: application_controller.rb helper_method :current_user private def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] end I have a list of assets that a user must be authorized to add. I am using paperclip. A user can has_many and a asset belongs_to a user (although

insert to database error on heroku but worked locally, ActiveRecord::StatementInvalid

落爺英雄遲暮 提交于 2019-12-25 06:51:05
问题 I have 2 models: class User < ActiveRecord::Base has_many :micro_posts, :dependent => :destroy .. def add_post(post) self.micro_posts << post end class MicroPost < ActiveRecord::Base belongs_to :user ... and the migration: class CreateMicroPosts < <ActiveRecord::Migration def change create_table :micro_posts do |t| ... And when I called add_post on heroku server, I got an error: 2012-02-06T02:59:58+00:00 app[web.1]: ActiveRecord::StatementInvalid (ArgumentError: wrong number of arguments (1

undefined method 'exec_prepared' on Rails 4 postgresql query

淺唱寂寞╮ 提交于 2019-12-25 06:38:19
问题 Every time, I submit a form supposed to create a Deal and sending a very high nb of Prizes (>200K) to the Prize table using a transaction and raw postgresql, I have first the error 'undefined method exec_prepared' then if I reload the form then I get a new error 'ERROR: prepared statement 'xixie' already exists'. I used this question wrong number of arguments (1 for 2..3) for Active Record postgresql query (Rails 4/postgresql 9.4) and Prepared Statement on Postgresql in Rails to create the

Hash/Array to Active Record

假如想象 提交于 2019-12-25 06:33:52
问题 I have been searching everywhere but I can't seem to find this problem anywhere. In Rails 5.0.0.beta3 I need to sort a my @record = user.records with an association and it's record. The sort goes something like this. @record = @record.sort_by { |rec| If user.fav_record.find(rec.id) User.fav_record(rec.id).created_at Else rec.created_at End This is just an example of what I do. But everything sorts fine. The problem: This returns an array and not an Active Record Class. I've tried everything

Cross uniqueness model validation in rails

独自空忆成欢 提交于 2019-12-25 06:04:15
问题 I have a model where I want columns to contain different ids. So a user may follow another user, but not follow themselves. Migration class CreateFollows < ActiveRecord::Migration def change create_table :follows do |t| t.integer :follower_id t.integer :followee_id t.timestamps end end end Model class Follow < ActiveRecord::Base belongs_to :follower, class_name: 'User' belongs_to :followee, class_name: 'User' validates :follower_id, uniqueness: { scope: :followee_id } end But my test seems to

Many to many through: Going from Associations::CollectionProxy to AssociationRelation

丶灬走出姿态 提交于 2019-12-25 05:06:18
问题 I have a many to many through relationship with users favoriting clients (Through favorites.rb) and I want to know how to turn a CollectionProxy into an AssociationRelation, so I can get a relation with all the clients that are the users' favorites. Or just simply, how to get them all in a AssociationRelation - doesn't have to be turned into from the CollectionProxy. EDIT: What I'm looking for is simply a relation that lists all the clients. I realize I asked the question differently. How do

newbie : getting mutual likes and only if they are mutual with active record?

岁酱吖の 提交于 2019-12-25 04:59:31
问题 What would be a good way to get mutual likes ? where 2 users like each other ( as in a friendlist) and get only those users who have a mutual match? user_id | likes_id 1 2 2 1 Only get results where both of the users likes each other ( friendship ) I have tried some join's but can't really get it to work, is there a building AR feature to get the above results? 回答1: To add to jexact's answer this is one way you can do this using AR: Like.joins('join likes l on likes.user_id=l.likes_id and l

How to adjust ActiveRecord (or straight SQL) query to include zero count records on a JOIN with a WHERE clause

橙三吉。 提交于 2019-12-25 04:58:33
问题 I have a question about forming a query in ActiveRecord but also provided the SQL for those that aren’t familiar with ActiveRecord. I have the following model class: class Shoe < ActiveRecord::Base has_many :purchases def self.available_shoes #show all shoes that have been purchased less than num_in_stock num_in_stock = 3 Shoe.includes(:purchases) .group("purchases.shoe_id") .having("COUNT(purchases.shoe_id) < ?", num_in_stock) end def self.available_shoes_for_user(user) #show all available

Use Ruby closure_tree gem without rails

人走茶凉 提交于 2019-12-25 04:58:08
问题 I want to try the gem closure_tree to build a tree for my genealogy database. I installed the gem under Ruby MRI 2.3.0 on a windows 7 box. The versions of the dependencies are in the errorlog below. $ gem install closure_tree Fetching: with_advisory_lock-3.0.0.gem (100%) Successfully installed with_advisory_lock-3.0.0 Fetching: closure_tree-6.2.0.gem (100%) Successfully installed closure_tree-6.2.0 2 gems installed When I try the most minimalistic code it fails, I suppose because I don't use