ruby-on-rails-4

Why does a Time math calculation return one value in IRB but sends a diff value to SQL?

我只是一个虾纸丫 提交于 2020-01-16 18:35:15
问题 I have a method in my Post model that looks like this: def self.num_posted_x_days_ago(n) start_date = Time.now.midnight - n.day end_date = Time.now.midnight - (n+1).day where(created_at: start_date..end_date).count end When I run it in IRB, I get this: > Post.num_posted_x_days_ago(8) (0.3ms) SELECT COUNT(*) FROM "posts" WHERE ("posts"."created_at" BETWEEN '2015-02-27 05:00:00.000000' AND '2015-02-26 05:00:00.000000') => 0 Note that the Time on the date is 05:00:00.000000 for both the start

Why does a Time math calculation return one value in IRB but sends a diff value to SQL?

北城以北 提交于 2020-01-16 18:35:13
问题 I have a method in my Post model that looks like this: def self.num_posted_x_days_ago(n) start_date = Time.now.midnight - n.day end_date = Time.now.midnight - (n+1).day where(created_at: start_date..end_date).count end When I run it in IRB, I get this: > Post.num_posted_x_days_ago(8) (0.3ms) SELECT COUNT(*) FROM "posts" WHERE ("posts"."created_at" BETWEEN '2015-02-27 05:00:00.000000' AND '2015-02-26 05:00:00.000000') => 0 Note that the Time on the date is 05:00:00.000000 for both the start

Rails 4 active record validation - conditionally validate presence of 4 attributes if at least one is present while allowing none to be present

末鹿安然 提交于 2020-01-16 18:07:07
问题 I have a form with 10 attributes. Among them I have 4 attributes which I need to apply what I'd call a"mutually conditional presence" Active Record validation. I want that (A) if one at least is present, then the other 3 must be present (B) still allow none to be present (if the other 3 are blank, then the fourth has the right be be blank) Here are the four attributes: address_line_1 zipcode state country It means that if the user fills ONE of them then ALL the others have to be present. But

Rails 4 active record validation - conditionally validate presence of 4 attributes if at least one is present while allowing none to be present

断了今生、忘了曾经 提交于 2020-01-16 18:07:07
问题 I have a form with 10 attributes. Among them I have 4 attributes which I need to apply what I'd call a"mutually conditional presence" Active Record validation. I want that (A) if one at least is present, then the other 3 must be present (B) still allow none to be present (if the other 3 are blank, then the fourth has the right be be blank) Here are the four attributes: address_line_1 zipcode state country It means that if the user fills ONE of them then ALL the others have to be present. But

Model from Gem is missing

空扰寡人 提交于 2020-01-16 17:31:26
问题 I am using mailboxer in my Rails 4 app. gem 'mailboxer' I recently updated it from 0.11 to 0.12.4 , it stopped working, and I cant figure out why. Now, I am getting the error: uninitialized constant Message I checked the gem's GitHub repo and it does have the Message model. I checked my local version of the gem and it matches up. Puma:mailboxer Jeff$ pwd /Users/Jeff/.rvm/gems/ruby-2.0.0-p247/gems/mailboxer-0.12.4/app/models/mailboxer Puma:mailboxer Jeff$ ls conversation conversation.rb

Rails 4: remove attribute name from error message in custom validator

寵の児 提交于 2020-01-16 04:30:26
问题 In my Rails 4 app, I implemented a custom validator named LinkValidator on my Post model: class LinkValidator < ActiveModel::Validator def validate(record) if record.format == "Link" if extract_link(record.copy).blank? record.errors[:copy] << 'Please make sure the copy of this post includes a link.' end end end end Everything works fine, except that currently, the message displayed is: 1 error prohibited this post from being saved: Copy Please make sure the copy of this post includes a link.

Add geometry field to PostGIS with Rails

廉价感情. 提交于 2020-01-16 02:54:08
问题 I have a database with a table called addresses. I want to add a geometry field. I could do it in pgAdmin, but not sure how it would play with Rails. I think sql would be: ALTER TABLE addresses ADD geom geometry(Point,4326) Then I will want to run UPDATE addresses SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326); Can I do that in Rails or must I do it in pgAdmin (or psql)? Thanks. Newbie getting lost in the complications of mashing several applications. 回答1: Are you using this gem

Seeding fails validation for nested tables (validates_presence_of)

帅比萌擦擦* 提交于 2020-01-16 02:52:26
问题 An Organization model has a 1:many association with a User model. I have the following validation in my User model file: belongs_to :organization validates_presence_of :organization_id, :unless => 'usertype==1' If usertype is 1, it means the user will have no organization associated to it. For a different usertype the presence of an organization_id should be mandatory. The organization model includes: has_many :users accepts_nested_attributes_for :users, :reject_if => :all_blank, :allow

simple_form undefined method `model_name' for NilClass:Class

最后都变了- 提交于 2020-01-16 02:28:10
问题 I'm learning Rails, and I would like to use simple_form. I'm creating a simple app (my first one), and I want to create a signup form. I used rails g scaffold User username:string password:string to create my model and controller. I'm using Foundation gem too, if that's matter, and I install simple_form with the right command for Foundation. I've been looking for answers for two hours, I tried many things, I have no idea of what's wrong with my code. ## app/views/home/index.html.erb ## <%=

Strong parameters with has_many gives “no implicit conversion of Symbol into Integer”

只谈情不闲聊 提交于 2020-01-16 01:26:44
问题 Trying to create a user from a json request but my server gives me "typeError (no implicit conversion of Symbol into Integer)". I understand that it's something wrong with my nested attribute but i dont know what, this's driving me crazy.. My Javascript file: user = { email: @get('email') first_name: @get('firstName') last_name: @get('lastName') password: @get('password') password_confirmation: @get('passwordConfirmation') registration_completed: true authentications_attributes: { provider: