I've used this forum gem before and also been able to set the forem_admin to "true" using the console. However, this time it's rolling back the transaction when I try to save.
You can see below that I set the forem_admin to "true," saved it, it rolled back the transaction (which means it didn't save), I did "u" and it showed forem_admin = true but when I restarted the console it showed forem_admin = false.
If anyone's familiar with this gem, I think this forem_state attribute is new, so I'm wondering if anything's changed.
I'm not an SQL pro but one thing that looks funny to me is the line
"users"."id" != 5
I would have expected it to have been "users"."id" = 5 i.e. with no "!", because the id of the user "signuplinks" is 5.
this is a rails 3.2 app
update when I do u.errors after u.save, I'm getting this error message
#messages={:password=>["can't be blank"]}
Console
ruby-1.9.3-rc1 :001 > u = User.last
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: false, forem_state: "pending_review">
ruby-1.9.3-rc1 :002 > u.forem_admin = true
=> true
ruby-1.9.3-rc1 :003 > u.save
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 FROM "users" WHERE (LOWER("users"."name") = LOWER('signuplinks') AND "users"."id" != 5) LIMIT 1
User Exists (0.1ms) SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('signuplinks#gmail.com') AND "users"."id" != 5) LIMIT 1
(0.1ms) rollback transaction
=> false
ruby-1.9.3-rc1 :004 > u
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: true, forem_state: "pending_review">
ruby-1.9.3-rc1 :005 > exit
Michael-Pro:ic michl$ rails c
Loading development environment (Rails 3.2.1)
ruby-1.9.3-rc1 :001 > u = User.last
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, email: "myemail#gmail.com", encrypted_password: "$2a$10$axjwcO.kU4/mqC9Llyj.b.r/2jJULnWKmG7Pi3Zu1AE3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-03-05 03:37:59", last_sign_in_at: "2012-03-05 03:37:59", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2012-03-05 03:37:58", confirmation_sent_at: "2012-03-05 03:34:33", unconfirmed_email: nil, created_at: "2012-03-05 03:34:33", updated_at: "2012-03-05 03:37:59", name: "signuplinks", country: "Canada", image: "3.png", forem_admin: false, forem_state: "pending_review">
actually, this was my own fault. Thanks to #muistooshort for pointing out problem probably is in model. I had included "password" in the validates_presence of validator which was stopping me from changing user details in the console (without including password)
Related
I want to have a feature where I can invite any user using only their phone_number. I don't have the email address. When a user is being invited, an SMS is sent to the specified number and depending upon the contents of the reply(say, 'Y' or 'N') SMS from user, their account is confirmed/rejected. We plan to provide a link in the sms where in the user can click and enter the required details(email, name etc).
Loading development environment (Rails 5.0.2)
2.3.0 :001 > u = User.invite!(phone_number: '1234567890')
=> #<User id: nil, email: nil, first_name: nil, last_name: nil, phone_number: "1234567890", created_at: nil, updated_at: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>
2.3.0 :002 > u.errors
=> #<ActiveModel::Errors:0x007fc91fcd4c00 #base=#<User id: nil, email: nil, first_name: nil, last_name: nil, phone_number: "1234567890", created_at: nil, updated_at: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>, #messages={:email=>["can't be blank"]}, #details={:email=>[{:error=>:blank}]}>
I have same problem and i set example email for it and remove it later...
u = User.invite!(phone_number: '1234567890', email: 'example#abc.com', skip_invitation: true)
u.update(email: '')
Add this to user.rb:
def email_required?
false
end
def email_changed?
false
end
I getting troubles with a self referential table.
I got an orb model which can hold planets, stars and moons. I want to tell that one thing "orbit" another
i look at the rails guide, but i coulnd get it working
My model:
class Orb < ActiveRecord::Base
belongs_to :orb_type
has_and_belongs_to_many :books
belongs_to :orbit, :class_name => "Orb"
has_many :orbs, :class_name => "Orb", :foreign_key => "orb_id"
attr_accessible :descr, :nome, :orb_type_id, :book_ids, :orb_id
validates :nome, uniqueness: true, presence: true
end
I think i am using bad relations names (maybe in the wrong way around)
1.9.3-p448 :002 > earth = Orb.find(1)
Orb Load (0.2ms) SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1 [["id", 1]]
=> #<Orb id: 1, nome: "Terra", descr: "123123", orb_type_id: 1, created_at: "2013-09-25 14:53:35", updated_at: "2013-09-25 14:57:40", orb_id: nil>
1.9.3-p448 :003 > moon = Orb.find(2)
Orb Load (0.2ms) SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1 [["id", 2]]
=> #<Orb id: 2, nome: "Lua", descr: "asd", orb_type_id: 2, created_at: "2013-09-25 14:53:46", updated_at: "2013-09-25 14:55:31", orb_id: nil>
1.9.3-p448 :004 > sun = Orb.find(3)
Orb Load (0.2ms) SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1 [["id", 3]]
=> #<Orb id: 3, nome: "Sol", descr: "asd", orb_type_id: 3, created_at: "2013-09-25 14:53:55", updated_at: "2013-09-25 14:53:55", orb_id: nil>
1.9.3-p448 :006 > moon.orbit=earth
=> #<Orb id: 1, nome: "Terra", descr: "123123", orb_type_id: 1, created_at: "2013-09-25 14:53:35", updated_at: "2013-09-25 14:57:40", orb_id: nil>
1.9.3-p448 :007 > earth.orbit=sun
=> #<Orb id: 3, nome: "Sol", descr: "asd", orb_type_id: 3, created_at: "2013-09-25 14:53:55", updated_at: "2013-09-25 14:53:55", orb_id: nil>
1.9.3-p448 :008 > earth
=> #<Orb id: 1, nome: "Terra", descr: "123123", orb_type_id: 1, created_at: "2013-09-25 14:53:35", updated_at: "2013-09-25 14:57:40", orb_id: nil>
1.9.3-p448 :009 > sun
=> #<Orb id: 3, nome: "Sol", descr: "asd", orb_type_id: 3, created_at: "2013-09-25 14:53:55", updated_at: "2013-09-25 14:53:55", orb_id: nil>
1.9.3-p448 :010 > moon
=> #<Orb id: 2, nome: "Lua", descr: "asd", orb_type_id: 2, created_at: "2013-09-25 14:53:46", updated_at: "2013-09-25 14:55:31", orb_id: nil>
in the end nothing get associated, the FK still nill.
The collun orb_id was added latter on the model. I setup a migration and added it in the model. I don't think it could be related to my problem...
EDIT:
Now everything is even odd. i change my model to:
class Orb < ActiveRecord::Base
belongs_to :orb_type
has_and_belongs_to_many :books
belongs_to :orbit, :class_name => "Orb"
has_many :orbits, :class_name => "Orb", :foreign_key => "orb_id"
attr_accessible :descr, :nome, :orb_type_id, :book_ids, :orb_id
validates :nome, uniqueness: true, presence: true
end
In rails console (rails c) i try:
1.9.3-p448 :008 > earth = Orb.find(1)
Orb Load (0.2ms) SELECT "orbs".* FROM "orbs" WHERE "orbs"."id" = ? LIMIT 1 [["id", 1]]
=> #<Orb id: 1, nome: "Terra", descr: "", orb_type_id: 1, created_at: "2013-09-25 17:51:26", updated_at: "2013-09-25 18:16:58", orb_id: 3>
1.9.3-p448 :009 > earth.orbit
=> nil
1.9.3-p448 :010 > earth.orbits
Orb Load (0.3ms) SELECT "orbs".* FROM "orbs" WHERE "orbs"."orb_id" = 1
=> [#<Orb id: 2, nome: "Lua", descr: "", orb_type_id: 2, created_at: "2013-09-25 17:51:40", updated_at: "2013-09-25 18:17:31", orb_id: 1>]
1.9.3-p448 :011 >
What the heck? orbits seen to return what i want, but it i try to use it:
1.9.3-p448 :004 > earth.orbits.nome
NoMethodError: Orb Load (0.4ms) SELECT "orbs".* FROM "orbs" WHERE "orbs"."orb_id" = 1
undefined method `nome' for #<ActiveRecord::Relation:0x00000003d593c0>
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.12/lib/active_record/relation/delegation.rb:45:in `method_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.12/lib/active_record/associations/collection_proxy.rb:100:in `method_missing'
from (irb):4
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Technically, it's not great that you have planets, stars, and moons all within the same model. They all behave differently and should each warrant their own model. E.g. a star will never orbit a planet, and a planet will never orbit a moon (by definition). By structuring your database the way you have, you're setting yourself up for corrupted data, which will always happen if you allow it to.
If you must have them all within the Orb model, I would recommend 2 models: 1 for the Orbs (stars, planets, moons), and 1 for the Orbits. The Orbit class would essentially be a table like so:
Orbit model:
| id | orbited_id | orbiter_id |
--------------------------------
| 0 | planet_id | moon_id |
| 1 | star_id | planet_id |
| 2 | planet_id | moon_id |
| ...| etc | etc |
And then you can set up the association in orb.rb:
has_and_belongs_to_many :orbits,
class_name: 'Orb',
join_table: :orbits,
foreign_key: :orbited_id,
association_foreign_key: :orbiter_id,
uniq: true
This will allow you to do things like
>> sun = Orb.find(1)
>> sun.orbits
=> [ <Orb mercury>, <Orb venus>, ..., <Orb pluto> ]
i figure it out. I should make the rom in the table withe the name of relation (orbit_id) instead of doing it the table name (orb_id)
Fixing that everything started to work nicely :)
has_many :orbters, class_name: "Orb", foreign_key: "orbit_id"
belongs_to :orbit, class_name: "Orb"
I discovered this bug while reproducing Michael Hartl's scaffolded rails demo_app from his rails tutorial.
I went into the rails console
rails c
I asked for the User.all array of hashes and out it through its paces:
2.0.0-p0 :012 > User.all
User Load (0.3ms) SELECT "users".* FROM "users"
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
2.0.0-p0 :013 > User.all[0]
User Load (0.2ms) SELECT "users".* FROM "users"
=> #<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">
2.0.0-p0 :015 > User.all[0]['id']
User Load (0.2ms) SELECT "users".* FROM "users"
=> 2
So far, so good.
However, User.all does not respond to the command to list say just the id's or names of all users:
2.0.0-p0 :017 > User.all { |i| puts i['id'] }
User Load (0.2ms) SELECT "users".* FROM "users"
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06"gt;]
2.0.0-p0 :019 >User.all{ |i| puts i['name'] }
User Load (0.3ms) SELECT "users".* FROM "users"
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
Assigning an arbitrary variable a to the array of hashes User.all resolves the issue:
2.0.0-p0 :021 >a.each {|i| puts i['id'] }
2
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
2.0.0-p0 :022 >a.each {|i| puts i['name'] }
Lisa Johnson
=> [#<User id: 2, name: "Lisa Johnson", email: "ljohnson#yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]
This User.all issue affects at least ruby versions 1.9.2,1.9.3 and 2.0.0. Whoever is responsible for writing the gem that created User.all needs to go over his all method. For whatever it's worth, I am working with rails 3.2.12
Nothing to discuss - its a straightforward bug report. If there is a question, the question would be "why is User.all is not behaving like the array of hashes it is?"
models:
class Person < ActiveRecord::Base
attr_accessible :email, :first, :last, :uuid, :books
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :person
attr_accessor :blurb, :published, :title, :person
attr_accessible :person
end
I created a Person using the Rails 3.2.8 console like this:
person = Person.create!( {:first => "John", :last => "Doe"} )
and then created a Book
book = Book.create!( {:title => "Ruby for Dummies"} )
I then try to associate them like this:
person.books << book
When I query the person for books, I get an array with the book I created, but when I query the book for the person it belongs to, I get nil. I expected to get the person, given that all information was persisted (I see the SQL commands and I checked the database and the data is correct, i.e. the row in the book table points back to the person id it should.)
What am I missing?
thanks
Edit- Schema:
CREATE TABLE `persons` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`first` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`last` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`uuid` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`email` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2;
CREATE TABLE `books` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`blurb` TEXT NULL COLLATE 'utf8_unicode_ci',
`published` TINYINT(1) NULL DEFAULT NULL,
`person_id` INT(11) NULL DEFAULT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3;
Assuming your migrations are correct, there is nothing wrong with your code which should produce the following results:
1.9.3p194 :001 > person = Person.create!( {:first => "John", :last => "Doe"} )
(0.1ms) begin transaction
SQL (145.9ms) INSERT INTO "people" ("book_id", "created_at", "email", "first", "last", "updated_at", "uuid") VALUES (?, ?, ?, ?, ?, ?, ?) [["book_id", nil], ["created_at", Mon, 03 Sep 2012 21:35:53 UTC +00:00], ["email", nil], ["first", "John"], ["last", "Doe"], ["updated_at", Mon, 03 Sep 2012 21:35:53 UTC +00:00], ["uuid", nil]]
(43.5ms) commit transaction
=> #<Person id: 1, email: nil, first: "John", last: "Doe", uuid: nil, book_id: nil, created_at: "2012-09-03 21:35:53", updated_at: "2012-09-03 21:35:53">
1.9.3p194 :005 > book = Book.create!( {:title => "Ruby for Dummies"} )
(0.1ms) begin transaction
SQL (1.4ms) INSERT INTO "books" ("blurb", "created_at", "person_id", "published", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["blurb", nil], ["created_at", Mon, 03 Sep 2012 21:36:25 UTC +00:00], ["person_id", nil], ["published", nil], ["title", nil], ["updated_at", Mon, 03 Sep 2012 21:36:25 UTC +00:00]]
(4.4ms) commit transaction
=> #<Book id: 1, blurb: nil, published: nil, title: nil, person_id: nil, created_at: "2012-09-03 21:36:25", updated_at: "2012-09-03 21:36:25">
1.9.3p194 :006 > person.books << book
(0.1ms) begin transaction
(0.3ms) UPDATE "books" SET "person_id" = 1, "updated_at" = '2012-09-03 21:36:39.566387' WHERE "books"."id" = 1
(4.4ms) commit transaction
Book Load (0.3ms) SELECT "books".* FROM "books" WHERE "books"."person_id" = 1
=> [#<Book id: 1, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:36:25", updated_at: "2012-09-03 21:36:39">]
1.9.3p194 :007 > Book.all
Book Load (0.5ms) SELECT "books".* FROM "books"
=> [#<Book id: 1, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:36:25", updated_at: "2012-09-03 21:36:39">]
The problem is with your Book model, try the following:
class Book < ActiveRecord::Base
attr_accessible :person, :title, :person
belongs_to :person
end
Before:
1.9.3p194 :002 > Book.last
Book Load (0.1ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
=> #<Book id: 4, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:55:33", updated_at: "2012-09-03 21:55:33">
1.9.3p194 :003 > Book.last.person
Book Load (0.2ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
=> nil
After:
1.9.3p194 :001 > Book.last
Book Load (0.4ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
=> #<Book id: 4, blurb: nil, published: nil, title: nil, person_id: 1, created_at: "2012-09-03 21:55:33", updated_at: "2012-09-03 21:55:33">
1.9.3p194 :002 > Book.last.person
Book Load (1.0ms) SELECT "books".* FROM "books" ORDER BY "books"."id" DESC LIMIT 1
Person Load (0.1ms) SELECT "people".* FROM "people" WHERE "people"."id" = 1 LIMIT 1
=> #<Person id: 1, email: nil, first: "John", last: "Doe", uuid: nil, created_at: "2012-09-03 21:48:02", updated_at: "2012-09-03 21:48:02">
Have you tried doing something like the following? If so, what does book.person give you?
person = Person.create!( {:first => "John", :last => "Doe"} )
book = Book.create!( {:title => "Ruby for Dummies"} )
book.person_id = person.id
person.books
book.person
In my seeds file I have the following:
puts "Creating Deans User"
user = User.create!(:email => "test#test.com", :password => "test1234", :password_confirmation => "test1234", :name => "Dean Chester", :admin => true)
puts "User created"
But when I check this in the console I see the following:
[#<User id: 1, email: "test#test.com", encrypted_password: "", 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: "2012-08-06 10:02:22", updated_at: "2012-08-06 10:02:22", admin: true, name: "Dean Chester">]
And the encrypted password field is blank so what is going wrong?
Do you have:
attr_accessor :password, :password_confirmation, :current_password
in your model? If so remove it and it will fix your issue.
You might be missing attr_accessible :password, :password_confirmation in your `User.rb.