How to express a NOT IN query with ActiveRecord/Rails?

I’m hoping there is a easy solution that doesn’t involve find_by_sql, if not then I guess that will have to work. I found this article which references this: Topic.find(:all, :conditions => { :forum_id => @forums.map(&:id) }) which is the same as SELECT * FROM topics WHERE forum_id IN (<@forum ids>) I am wondering if there … Read more

ActiveModel::ForbiddenAttributesError when creating new user

I have this model in Ruby but it throws a ActiveModel::ForbiddenAttributesError class User < ActiveRecord::Base attr_accessor :password validates :username, :presence => true, :uniqueness => true, :length => {:in => 3..20} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, :uniqueness => true, format: { with: VALID_EMAIL_REGEX } validates :password, :confirmation => true validates_length_of :password, :in => 6..20, … Read more

Float vs Decimal in ActiveRecord

Sometimes, Activerecord data types confuse me. Err, often. One of my eternal questions is, for a given case, Should I use :decimal or :float? I’ve often come across this link, ActiveRecord: :decimal vs :float?, but the answers aren’t quite clear enough for me to be certain: I’ve seen many threads where people recommend flat out … Read more

Ruby on Rails generates model field:type – what are the options for field:type?

I’m trying to generate a new model and forget the syntax for referencing another model’s ID. I’d look it up myself, but I haven’t figured out, among all my Ruby on Rails documentation links, how to find the definitive source. $ rails g model Item name:string description:text (and here either reference:product or references:product). But the … Read more

Add a reference column migration in Rails 4

A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like? Here is what I have. I’m not sure if I should use (1) :user_id, :int or (2) :user, :references. I’m not even sure if (2) works. Just trying to do … Read more

Rails where condition using NOT NIL

Using the rails 3 style how would I write the opposite of: Foo.includes(:bar).where(:bars=>{:id=>nil}) I want to find where id is NOT nil. I tried: Foo.includes(:bar).where(:bars=>{:id=>!nil}).to_sql But that returns: => “SELECT \”foos\”.* FROM \”foos\” WHERE (\”bars\”.\”id\” = 1)” That’s definitely not what I need, and almost seems like a bug in ARel. 5 Answers 5

Rails update_attributes without save?

Is there an alternative to update_attributes that does not save the record? So I could do something like: @car = Car.new(:make => ‘GMC’) #other processing @car.update_attributes(:model => ‘Sierra’, :year => “2012”, :looks => “Super Sexy, wanna make love to it”) #other processing @car.save BTW, I know I can @car.model=”Sierra”, but I want to update them … Read more