Related
Model:
class Reservation < ApplicationRecord
# https://naturaily.com/blog/ruby-on-rails-enum
enum recurrence: {
daily: 0,
weekly: 1,
monthly: 2,
annually: 3
}, _prefix: :recurring
belongs_to :user
validates :name, :user_id, presence: true
...
end
Migration:
class CreateReservations < ActiveRecord::Migration[5.2]
def change
create_table :reservations do |t|
t.string 'name', null: false
...
t.boolean 'recurring', default: false, null: false
t.integer 'recurrence', index: true, allow_blank: true, default: nil # trying a lot of things here
t.datetime 'expire_time'
...
end
end
end
Works as expected in the console:
2.4.5 :002 > res = Reservation.new(name: 'test', user_id: 1)
=> #<Reservation id: nil, name: "test", recurring: false, recurrence: nil, date: nil, start_time: nil, end_time: nil, expire_time: nil, user_id: 1, created_at: nil, updated_at: nil>
2.4.5 :003 > res.valid?
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
=> true
2.4.5 :004 > res.save
(0.2ms) BEGIN
Reservation Create (0.5ms) INSERT INTO "reservations" ("name", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "test"], ["user_id", 1], ["created_at", "2019-09-24 20:42:40.933959"], ["updated_at", "2019-09-24 20:42:40.933959"]]
(2.1ms) COMMIT
=> true
2.4.5 :005 > res.reload
Reservation Load (0.3ms) SELECT "reservations".* FROM "reservations" WHERE "reservations"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]]
=> #<Reservation id: 5, name: "test", recurring: false, recurrence: nil, date: nil, start_time: nil, end_time: nil, expire_time: nil, user_id: 1, created_at: "2019-09-24 20:42:40", updated_at: "2019-09-24 20:42:40">
2.4.5 :006 > res.recurrence
=> nil
And yet, in Rails Admin, when I create or edit a record, they all get assigned to the first enum. Even when I intentionally delete the value from the form, it still saves the record with the first enum value.
Rails Admin:
config.model Reservation do
weight 2
parent Event
list do
field :name
field :display_date do
formatted_value { bindings[:object].display_date }
end
field :recurrence, :active_record_enum # should be unnecessary, but trying everything
field :expire_time
field :user do
label 'Creator'
formatted_value { bindings[:object].user.name }
end
end
end
I have tried:
not indexing this field (thinking Rails Admin wants a value for indexed columns)
ensuring `:active_record_enum` is declared on `field :recurrence`
As suggested, here's the related section of the log:
Started POST "/admin/reservation/new" for ::1 at 2019-09-25 10:09:50 -0400
Processing by RailsAdmin::MainController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VbhHM7YD+oQvwMYIQzMHyT5e0MXFFuN3pyH/1s6yM8n6kqKvywZ5E8zaABMKhOU+oWpfU1Kk55FWYjL9TzbkbQ==", "reservation"=>{"name"=>"logs", "date"=>"", "start_time"=>"", "end_time"=>"", "recurring"=>"0", "recurrence"=>"", "expire_time"=>"", "start_time_of_day"=>"", "end_time_of_day"=>"", "day_of_week"=>"", "date_of_month"=>"", "date_of_year"=>"", "user_id"=>"1"}, "return_to"=>"http://localhost:3000/admin/reservation", "_save"=>"", "model_name"=>"reservation"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Reservation Create (0.4ms) INSERT INTO "reservations" ("name", "recurrence", "day_of_week", "date_of_month", "date_of_year", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["name", "logs"], ["recurrence", 0], ["day_of_week", ""], ["date_of_month", ""], ["date_of_year", ""], ["user_id", 1], ["created_at", "2019-09-25 14:09:50.926864"], ["updated_at", "2019-09-25 14:09:50.926864"]]
(1.3ms) COMMIT
Redirected to http://localhost:3000/admin/reservation
Completed 302 Found in 26ms (ActiveRecord: 3.0ms)
And, sure enough the param "recurrence"=>"" is transformed into ["recurrence", 0]
It seems to be a known issue, but if it's a legit bug, I still need help with a work-around. Anyone solved this already?
This technically works, but I'm thinking it's not the ideal solution:
If you declare your enum as a hash (not an array) and don't assign anything to 0, it works.
In my case enum recurrence: { daily: 0, weekly: 1, monthly: 2, annually: 3 } becomes enum recurrence: { daily: 1, weekly: 2, monthly: 3, annually: 4 }
The logs show the param isn't passed to the SQL:
Started POST "/admin/reservation/new" for ::1 at 2019-09-25 10:21:17 -0400
Processing by RailsAdmin::MainController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Joyg5VuquwMMKg+ps2zg9t+8r0DZ0iOIkqRlk1cBkemJpkV5Jq84lO8wybL62wIBQIgg1k5gJ25j56i41oVGTQ==", "reservation"=>{"name"=>"test3", "date"=>"", "start_time"=>"", "end_time"=>"", "recurring"=>"0", "recurrence"=>"", "expire_time"=>"", "start_time_of_day"=>"", "end_time_of_day"=>"", "day_of_week"=>"", "date_of_month"=>"", "date_of_year"=>"", "user_id"=>"1"}, "return_to"=>"http://localhost:3000/admin/reservation", "_save"=>"", "model_name"=>"reservation"}
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL AND "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Reservation Create (1.3ms) INSERT INTO "reservations" ("name", "day_of_week", "date_of_month", "date_of_year", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "test3"], ["day_of_week", ""], ["date_of_month", ""], ["date_of_year", ""], ["user_id", 1], ["created_at", "2019-09-25 14:21:17.091292"], ["updated_at", "2019-09-25 14:21:17.091292"]]
(5.7ms) COMMIT
The param contains "recurrence"=>"", but the INSERT INTO doesn't have an array for recurrence like it did before.
And I confirmed in the console:
2.4.5 :001 > res = Reservation.first
Reservation Load (0.4ms) SELECT "reservations".* FROM "reservations" ORDER BY "reservations"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Reservation id: 4, name: "test3", date: nil, start_time: nil, end_time: nil, recurring: false, recurrence: nil, expire_time: nil, start_time_of_day: nil, end_time_of_day: nil, day_of_week: "", date_of_month: "", date_of_year: "", user_id: 1, created_at: "2019-09-25 14:21:17", updated_at: "2019-09-25 14:21:17">
2.4.5 :002 > res.recurrence
=> nil
However, it seems to me like I'm just passing a dis-allowed value and letting Rails reject it. This still seems to be an issue with Rails Admin, specifically with integer enums.
It could be a restriction of using hashes, because nil values should work. Since you're mapping those to 0-indexed values, you should just use a symbol array. The ActiveRecord::Enum documentation (linked above) states:
Note that when an array is used, the implicit mapping from the values to database integers is derived from the order the values appear in the array. In the example, :active is mapped to 0 as it's the first element, and :archived is mapped to 1. In general, the i-th element is mapped to i-1 in the database.
So you should be able to just use an array of symbols for this.
A user can have many questions. Many users can participate on a question and provide multiple answers to the same question. Relation between users and questions seems to be working fine. Problem is with answers.
model/user.rb
has_and_belongs_to_many :questions
has_many :answers
model/question.rb
has_and_belongs_to_many :users
has_many :answers
model/answer.rb
has_one :question
has_one :user, :through => :question
Here is what I'm trying in the console:
irb(main):022:0> u1.questions.first.answers.create(answer: "foo1", order:1)
(0.2ms) BEGIN
Answer Exists (0.5ms) SELECT 1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2 [["answer", "foo1"], ["LIMIT", 1]]
SQL (0.4ms) INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", 2017-03-31 19:31:04 UTC], ["updated_at", 2017-03-31 19:31:04 UTC], ["answer", "foo1"], ["order", 1], ["question_id", 1]]
(13.5ms) COMMIT
=> #<Answer id: 6, created_at: "2017-03-31 19:31:04", updated_at: "2017-03-31 19:31:04", answer: "foo1", order: 1, user_id: nil, question_id: 1>
irb(main):023:0> u1.questions.first.answers.create(answer: "bar1", order:2)
(0.4ms) BEGIN
Answer Exists (0.9ms) SELECT 1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2 [["answer", "bar1"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", 2017-03-31 19:31:04 UTC], ["updated_at", 2017-03-31 19:31:04 UTC], ["answer", "bar1"], ["order", 2], ["question_id", 1]]
(19.9ms) COMMIT
=> #<Answer id: 7, created_at: "2017-03-31 19:31:04", updated_at: "2017-03-31 19:31:04", answer: "bar1", order: 2, user_id: nil, question_id: 1>
irb(main):024:0> u2.questions.first.answers.create(answer: "foo2", order:1)
(0.7ms) BEGIN
Answer Exists (0.6ms) SELECT 1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2 [["answer", "foo2"], ["LIMIT", 1]]
SQL (0.9ms) INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", 2017-03-31 19:31:04 UTC], ["updated_at", 2017-03-31 19:31:04 UTC], ["answer", "foo2"], ["order", 1], ["question_id", 1]]
(16.7ms) COMMIT
=> #<Answer id: 8, created_at: "2017-03-31 19:31:04", updated_at: "2017-03-31 19:31:04", answer: "foo2", order: 1, user_id: nil, question_id: 1>
irb(main):025:0> u2.questions.first.answers.create(answer: "bar2", order:2)
(0.5ms) BEGIN
Answer Exists (0.6ms) SELECT 1 AS one FROM "answers" WHERE "answers"."answer" = $1 LIMIT $2 [["answer", "bar2"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "answers" ("created_at", "updated_at", "answer", "order", "question_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", 2017-03-31 19:31:05 UTC], ["updated_at", 2017-03-31 19:31:05 UTC], ["answer", "bar2"], ["order", 2], ["question_id", 1]]
(14.9ms) COMMIT
=> #<Answer id: 9, created_at: "2017-03-31 19:31:05", updated_at: "2017-03-31 19:31:05", answer: "bar2", order: 2, user_id: nil, question_id: 1>
It's obvious something is wrong given the fact user_id is null, unless I specify. I expect user_id populated given the fact I'm creating it from a user object through a question.
irb(main):027:0* u1.questions.first.answers.count
(0.7ms) SELECT COUNT(*) FROM "answers" WHERE "answers"."question_id" = $1 [["question_id", 1]]
=> 4
As I can see here, it just filters by question_id. I expect to retrieve the answers from question_id AND user_id
Can this problem be fixed? Should I just do it in another way?
Thanks
UPDATE
As a summary, this is what I would like to achieve:
User.first.answers.where(question_id: 1)
Where the query looks like:
SELECT "answers".* FROM "answers" WHERE "answers"."user_id" = $1 AND "answers"."question_id" = $2 [["user_id", 1], ["question_id", 1]]
I would like to be able to do:
User.first.questions.first.answers
And get the answers for user_id and question_id
Thanks
I think you should use belongs_to for :user and :question (instead of has_one) and set user manually to your answers from current_user in controller.
I am using Devise for my user authentication and would like to destroy an associated profile along with the user.
My failing spec looks like this:
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(profile).to be_nil
end
And
In my user model:
has_one :profile, dependent: :destroy
In my profile model:
belongs_to :user
In the console, I can reproduce the issue like this:
2.0.0p247 :001 > #user = FactoryGirl.create(:user)
(1.5ms) BEGIN
User Exists (2.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'person946979#example.com' LIMIT 1
User Exists (1.7ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('person946979#example.com') LIMIT 1
SQL (15.7ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["email", "person946979#example.com"], ["encrypted_password", "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5z3G77Dgja"], ["name", "Example User"], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00]]
SQL (3.8ms) INSERT INTO "profiles" ("created_at", "updated_at", "user_id") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["user_id", 25]]
Profile Load (3.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 25]]
(2.2ms) COMMIT
=> #<User id: 25, email: "person946979#example.com", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User">
2.0.0p247 :002 > #user.destroy
(1.0ms) BEGIN
SQL (2.5ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 4]]
SQL (5.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 25]]
(2.0ms) COMMIT
=> #<User id: 25, email: "person946979#example.com", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User">
Interestingly, the user appears to actually have been deleted.
2.0.0p247 :003 > #user.reload.destroy
User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 25]]
ActiveRecord::RecordNotFound: Couldn't find User with id=25
What is going on here?
Your model seems fine. Try something like this:
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(Profile.find(profile.id)).to be_nil
end
Like Heungju said, while the database row that corresponds to profile is being destroyed, the variable itself isn't.
How about this?
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(#user.profile).to be_nil
end
After #user.destroy, the 'profile', variable that expected to be nil, was not changed. I think...
Rewriting my spec like this does what I need:
it "should destroy associated profile" do
expect {
#user.destroy
}.to change(Profile, :count).by(-1)
end
i've been searching around for some time now and can't seem to figure out what is causing this, I had a model working saving a volunteer and his home and work addresses. I changed something (cant seem to figure out what for the life of me) and now the address isnt being set.
This is my main model:
class HumanVolunteer < ActiveRecord::Base
has_one :primaryPhone, :class_name => "PhoneNumber", :foreign_key => "id", :primary_key => "phone_id"
has_one :primaryEmail, :class_name => "Email", :foreign_key => "id", :primary_key => "email_id"
has_one :work_adr, :class_name => "Address", :foreign_key => "id", :primary_key => "workaddressid"
has_one :home_adr, :class_name => "Address", :foreign_key => "id", :primary_key => "homeaddressid"
attr_accessible :firstName, :lastName, :homeaddressid, :notes, :status, :workaddressid, :home_adr, :work_adr,
:primaryPhone, :primaryEmail, :home_adr_attributes, :work_adr_attributes, :primaryPhone_attributes,
:primaryEmail_attributes
accepts_nested_attributes_for :home_adr
accepts_nested_attributes_for :work_adr
accepts_nested_attributes_for :primaryPhone
accepts_nested_attributes_for :primaryEmail
end
and the address model:
class Address < ActiveRecord::Base
attr_accessible :city, :line1, :line2, :notes, :state, :zipcode
belongs_to :human_volunteer
end
Heres a snippet from the human_volunteers_controller
# GET /human_volunteers/new
# GET /human_volunteers/new.json
def new
#human_volunteer = HumanVolunteer.new
#human_volunteer.build_home_adr
#human_volunteer.build_work_adr
#human_volunteer.build_primaryPhone
#human_volunteer.build_primaryEmail
# Set the Breadcrumbs for this page
#breadcrumbs = { "Dashboard" => "/", "Human Volunteers" => "/human_volunteers"}
#current_page = "New Volunteer"
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #human_volunteer }
end
end
# GET /human_volunteers/1/edit
def edit
#human_volunteer = HumanVolunteer.find(params[:id])
if(#human_volunteer.home_adr.nil?)
#human_volunteer.build_home_adr
end
if(#human_volunteer.work_adr.nil?)
#human_volunteer.build_work_adr
end
if(#human_volunteer.primaryPhone.nil?)
#human_volunteer.build_primaryPhone
end
if(#human_volunteer.primaryEmail.nil?)
#human_volunteer.build_primaryEmail
end
#breadcrumbs = { "Dashboard" => "/", "Human Volunteers" => "/human_volunteers"}
#current_page = "Edit Volunteer: <em>" + #human_volunteer.firstName + " " + #human_volunteer.lastName + "</em>";
end
# POST /human_volunteers
# POST /human_volunteers.json
def create
# create the volunteer
#human_volunteer = HumanVolunteer.new(params[:human_volunteer])
respond_to do |format|
if #human_volunteer.save
format.html { redirect_to #human_volunteer, :notice => #human_volunteer.firstName + ' ' + #human_volunteer.lastName + ' was successfully created.' }
format.json { render :json => #human_volunteer, :status => :created, :location => #human_volunteer }
else
format.html { render :action => "new" }
format.json { render :json => #human_volunteer.errors, :status => :unprocessable_entity }
end
end
end
# PUT /human_volunteers/1
# PUT /human_volunteers/1.json
def update
#human_volunteer = HumanVolunteer.find(params[:id])
respond_to do |format|
if #human_volunteer.update_attributes(params[:human_volunteer])
format.html { redirect_to #human_volunteer, :notice => #human_volunteer.firstName + ' ' + #human_volunteer.lastName + ' was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #human_volunteer.errors, :status => :unprocessable_entity }
end
end
end
I can see what i think the problem is by looking at the logs, the human volunteer get saved first, using nulls for the address keys, then the addresses are saved ( also same problem with phone number and email models, they are set up identically to the addresses ) then the addresses are saved, and then the human volunteer is never updated again with the new values:
Parameters: {"authenticity_token"=>"dD+ut6noFPw8mJHq4rUJpuNBD1o+q0Hi8a+qOeetzMc=", "utf8"=>"✓", "human_volunteer"=>{"lastName"=>"Koch", "work_adr_attributes"=>{"line1"=>"", "line2"=>"", "zipcode"=>"", "state"=>"", "city"=>""}, "primaryEmail_attributes"=>{"email"=>""}, "primaryPhone_attributes"=>{"number"=>""}, "notes"=>"", "home_adr_attributes"=>{"line1"=>"123 Who Knows land", "line2"=>"", "zipcode"=>"11741", "state"=>"TS", "city"=>"testville"}, "firstName"=>"Ken", "status"=>"Investigative Candidate"}}
(0.2ms) BEGIN
(0.2ms) COMMIT
(0.2ms) BEGIN
(0.1ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
(0.1ms) BEGIN
SQL (2.3ms) INSERT INTO "human_volunteers" ("created_at", "email_id", "firstName", "homeaddressid", "lastName", "notes", "phone_id", "status", "updated_at", "workaddressid") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["email_id", nil], ["firstName", "Ken"], ["homeaddressid", nil], ["lastName", "Koch"], ["notes", ""], ["phone_id", nil], ["status", "Investigative Candidate"], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["workaddressid", nil]]
SQL (0.8ms) INSERT INTO "phone_numbers" ("created_at", "notes", "number", "phone_id", "phone_type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["notes", nil], ["number", ""], ["phone_id", nil], ["phone_type", nil], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00]]
SQL (0.7ms) INSERT INTO "emails" ("created_at", "email", "notes", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["email", ""], ["notes", nil], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00]]
SQL (0.9ms) INSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["city", ""], ["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["line1", ""], ["line2", ""], ["notes", nil], ["state", ""], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["zipcode", nil]]
SQL (0.5ms) INSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["city", "testville"], ["created_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["line1", "123 Who Knows land"], ["line2", ""], ["notes", nil], ["state", "TS"], ["updated_at", Tue, 07 Aug 2012 16:47:16 UTC +00:00], ["zipcode", 11741]]
(17.8ms) COMMIT
EDIT: If this is any help here is an example of the old entry in the log:
Started POST "/human_volunteers" for 127.0.0.1 at Sat Jul 28 09:08:59 -0400 2012
Processing by HumanVolunteersController#create as HTML
Parameters: {"human_volunteer"=>{"firstName"=>"Ken", "work_adr"=>{"city"=>"", "state"=>"", "zipcode"=>"", "line1"=>"", "line2"=>""}, "status"=>"Trainee", "home_adr"=>{"city"=>"fdsa", "state"=>"Nasdf", "zipcode"=>"11729", "line1"=>"asdf fdsafd", "line2"=>""}, "primaryEmail"=>"ken.koch#essencedesigns.net", "primaryPhone"=>"6316813806", "lastName"=>"Koch", "notes"=>"\r\nHes a good guy"}, "authenticity_token"=>"bgbecE+nMxNrPtleNOLfO/MqRib0cXmDMqL5JaPyC10=", "utf8"=>"✓"}
[1m[36m (0.1ms)[0m [1mBEGIN[0m
[1m[35mSQL (0.5ms)[0m INSERT INTO "emails" ("created_at", "email", "notes", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["email", "ken.koch#essencedesigns.net"], ["notes", nil], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00]]
[1m[36m (19.1ms)[0m [1mCOMMIT[0m
[1m[35m (0.1ms)[0m BEGIN
[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "phone_numbers" ("created_at", "notes", "number", "phone_id", "phone_type", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"[0m [["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["notes", nil], ["number", "6316813806"], ["phone_id", nil], ["phone_type", nil], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00]]
[1m[35m (56.9ms)[0m COMMIT
[1m[36m (0.1ms)[0m [1mBEGIN[0m
[1m[35mSQL (2.5ms)[0m INSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["city", ""], ["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["line1", ""], ["line2", ""], ["notes", nil], ["state", ""], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["zipcode", nil]]
[1m[36m (9.4ms)[0m [1mCOMMIT[0m
[1m[35m (0.1ms)[0m BEGIN
[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "addresses" ("city", "created_at", "line1", "line2", "notes", "state", "updated_at", "zipcode") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"[0m [["city", "Deer Park"], ["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["line1", "294 West 15th St"], ["line2", ""], ["notes", nil], ["state", "NY"], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["zipcode", 11729]]
[1m[35m (7.0ms)[0m COMMIT
[1m[36m (0.1ms)[0m [1mBEGIN[0m
[1m[35mSQL (1.1ms)[0m INSERT INTO "human_volunteers" ("created_at", "email_id", "firstName", "homeaddressid", "lastName", "notes", "phone_id", "status", "updated_at", "workaddressid") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["created_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["email_id", 5], ["firstName", "Ken"], ["homeaddressid", 14], ["lastName", "Koch"], ["notes", "\r\nHes a good guy"], ["phone_id", 11], ["status", "Trainee"], ["updated_at", Sat, 28 Jul 2012 13:08:59 UTC +00:00], ["workaddressid", 13]]
[1m[36m (6.1ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/human_volunteers/1
Completed 302 Found in 178ms (ActiveRecord: 112.1ms)
I was able to look back into the log and find one that worked, and the human_volunteer was the last thing inserted. It inserted addresses, emails, phones first, then used those id's when inserting the volunteer.
EDIT: I can also report that I am not getting any errors, everything goes through, just the data isnt connected.
Any ideas what could cause the change in the order? I can't see that i changed anything that affected this.
UPDATE: I tried changing the has_one lines and was able to at least produce an error (for better or worse), when i cahnged the lines to:
has_one :workaddress, :class_name => "Address", :foreign_key => "id", :primary_key => "workaddress"
has_one :homeaddress, :class_name => "Address", :foreign_key => "id", :primary_key => "homeaddress"
It tries to use the addressid 1 everytime and i get
ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint "addresses_pkey"
DETAIL: Key (id)=(1) already exists.
Also if i take out the primary_key field, it seems to work, but it tries to set them both to the same address and i get the same error except with the next id (22, 23, 24 etc each time i try it)
Maybe that sheds some light on the problem? I'm going to keep prodding around.
Thank you very much,
- Ken
I came up with what seems to be a viable solution.
Since once the id was initially set, everything seemed to work, i just manually linked it up in the controller under the create action:
#human_volunteer.homeaddress.save
#human_volunteer.homeaddressid = #human_volunteer.homeaddress.id
#human_volunteer.workaddress.save
#human_volunteer.workaddressid = #human_volunteer.workaddress.id
Now the address gets set when the volunteer is created and everything else should carry out nicely.
The only additional consideration i can see would be making sure that when i add validators to the address that the addresses can actually validate before allowing the creation of the entire thing.
Thanks everyone,
-Ken
I am making a Ruby on Rails app and am having trouble setting some attributes of my model. The problem is that I have a :before_save method, yet for some reason the encrypted_password and salt aren't getting saved to the database. Here's the model:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :username, :email, :password
before_save :encrypt_password
...
private
def encrypt_password
logger.debug "Got to encrypt_password 1"
logger.debug "encrypted_password is #{ #encrypted_password }"
if #encrypted_password != nil and #encrypted_password != ""
return # #encrypted_password was already set
end
logger.debug "Got to encrypt_password 2"
#salt = make_salt if new_record?
logger.debug "New salt = #{ #salt }"
#encrypted_password = encrypt(#password)
logger.debug "New encrypted_password = #{ #encrypted_password }"
#password = "(encrypted)"
logger.debug "Got to encrypt_password 3"
end
In the log file, I see the following:
Started POST "/users" for 127.0.0.1 at Wed May 02 22:55:20 -0500 2012
Processing by UsersController#create as HTML
Parameters: {"commit"=>"Submit", "authenticity_token"=>"RY9fSMqb2+tdQ0fIjiEz8cfMTWTi012vCWdCvbxACLk=", "utf8"=>"\342\234\223", "user"=>{"username"=>"test6", "password"=>"[FILTERED]", "email"=>"test6#gmail.com"}}
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mUser Exists (0.2ms)[0m SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('test6#gmail.com') LIMIT 1
Got to encrypt_password 1
encrypted_password is
Got to encrypt_password 2
New salt = 4f3464029393829aa562e533773f668c8471c51231611f6f214e654275f37184
New encrypted_password = 0dafcff2fe75bb6f2b53afda79789cfe13bd3f733b817a0e2e30df98af5829bc
Got to encrypt_password 3
[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "users" ("created_at", "email", "encrypted_password", "is_done", "last_seen", "salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m [["created_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["email", "test6#gmail.com"], ["encrypted_password", nil], ["is_done", false], ["last_seen", nil], ["salt", nil], ["updated_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["username", "test6"]]
[1m[35m (0.7ms)[0m commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 7ms (ActiveRecord: 1.4ms)
So it's definitely making the salt and the encrypted password. Yet the database isn't getting updated?!
>> User.find(6)
=> #<User id: 6, username: "test6", email: "test6#gmail.com", encrypted_password: nil, is_done: false, salt: nil, last_seen: nil, created_at: "2012-05-03 03:55:20", updated_at: "2012-05-03 03:55:20">
Try using self.encrypted_password.
ActiveRecord makes getter/setter methods for your attributes, i.e.
def encrypted_password
...
# return something from the db
end
def encrypted_password=(x)
...
# set something in the db to x
end
And when you write #encrypted_password, you're not actually using these methods, so the database doesn't get updated.
You can see this in your log:
[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "users" ("created_at", "email", "encrypted_password", "is_done", "last_seen", "salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m [["created_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["email", "test6#gmail.com"], ["encrypted_password", nil], ["is_done", false], ["last_seen", nil], ["salt", nil], ["updated_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["username", "test6"]]
salt and encrpyed_password is being set to nil, because you didn't update the attribute, you updated a class member variable.
I think you forgot self.encrypted_password = #encrypted_password at the end of def encrypt_password.