Rails: Using greater than/less than with a where statement
Try this User.where(“id > ?”, 200)
Try this User.where(“id > ?”, 200)
If you want to use an OR operator on one column’s value, you can pass an array to .where and ActiveRecord will use IN(value,other_value): Model.where(:column => [“value”, “other_value”] outputs: SELECT `table_name`.* FROM `table_name` WHERE `table_name`.`column` IN (‘value’, ‘other_value’) This should achieve the equivalent of an OR on a single column
Just a note that the currently accepted answer is deprecated in Rails 3. You should do this instead: Comment.where(:created_at => @selected_date.beginning_of_day..@selected_date.end_of_day) Or, if you want to or have to use pure string conditions, you can do: Comment.where(‘created_at BETWEEN ? AND ?’, @selected_date.beginning_of_day, @selected_date.end_of_day)
You can scope a validates_uniqueness_of call as follows. validates_uniqueness_of :user_id, :scope => :friend_id
Sometimes, you want to use different names for different associations. If the name you want to use for an association on the model isn’t the same as the assocation on the :through model, you can use :source to specify it. I don’t think the above paragraph is much clearer than the one in the docs, … Read more
I believe the following should work for you. Event.includes(users: :profile) If you want to include an association (we’ll call it C) of an already included association (we’ll call it B), you’d use the syntax above. However, if you’d like to include D as well, which is also an association of B, that’s when you’d use … Read more
Rails 4+: Article.where.not(title: [‘Rails 3’, ‘Rails 5’]) Rails 3: Topic.where(‘id NOT IN (?)’, Array.wrap(actions)) Where actions is an array with: [1,2,3,4,5]
You should read that, it’s still valid. You’ll adapt the function you use depending on your needs. Basically: if you already load all entries, say User.all, then you should use length to avoid another db query if you haven’t anything loaded, use count to make a count query on your db if you don’t want … Read more
I guess you are using Rails 4. If so, the needed parameters must be marked as required. You might want to do it like this: class UsersController < ApplicationController def create @user = User.new(user_params) # … end private def user_params params.require(:user).permit(:username, :email, :password, :salt, :encrypted_password) end end
I remember my CompSci professor saying never to use floats for currency. The reason for that is how the IEEE specification defines floats in binary format. Basically, it stores sign, fraction and exponent to represent a Float. It’s like a scientific notation for binary (something like +1.43*10^2). Because of that, it is impossible to store … Read more