I'm trying to set up a model using UUIDs instead of auto-increment. Without bothering with the UUID part, here is what my code looks like:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users, :id => false do |t|
t.column :id, 'char(36) binary', :null => false, :primary => true
t.string :email
t.string :username
t.string :password_hash
t.string :password_salt
t.timestamps
end
end
end
The user model just has:
class User < ActiveRecord::Base
end
When I try to assign the id in the console it doesn't take:
ruby-1.9.3-p0 > u = User.new
=> #<User id: nil, email: nil, username: nil, password_hash: nil, password_salt: nil, created_at: nil, updated_at: nil>
ruby-1.9.3-p0 > u.id = 'stuff'
=> "stuff"
ruby-1.9.3-p0 > u.id
=> nil
ruby-1.9.3-p0 > u.id = UUIDTools::UUID.random_create.to_s
=> "d3cd5bef-22b4-4fb1-b00f-10d8bbc94dc1"
ruby-1.9.3-p0 > u.id
=> nil
ruby-1.9.3-p0 > u.email = 'stuff'
=> "stuff"
ruby-1.9.3-p0 > u.email
=> "stuff"
Why can I not assign the id manually? If I name the column something else it works, but I'd prefer to be able to use the id field so I don't have to override all the rails magic. It seems to work fine in Rails 2. Is there some Rails setting that protects the id from changes?
Try using this:
set_primary_key :id
in your models.
There's also a activeuuid plugin.
This SO post seems to back up the set_primary_key thing, at least partially.
Related
This is what I've tried, and I'm not sure where the problem is. . .
My model:
class User < ActiveRecord::Base
acts_as_authentic
has_many :assignments
end
Migration:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :login, :null => false
t.string :crypted_password, :null => false
t.string :password_salt, :null => false
t.string :persistence_token, :null => false
t.timestamps null: false
end
end
end
This is what I'm getting in Rails console when trying to create a user:
`<main>'2.2.0 :004 > User.create(login:'whatever', password:'1', password_confirmation:'1')
(0.1ms) begin transaction
User Exists (1.1ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."login") = LOWER('whatever') LIMIT 1
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."persistence_token" = '4886c517de2414c730b7ed5cc296b0e19c3e56ee398fb0aeed0531c55cbb04fd2a8ce577a4d5bd7dbb52e46cf9ab5e27e287af0993a708d07ded12553460d11c' LIMIT 1
(0.1ms) rollback transaction
=> #<User id: nil, login: "whatever", crypted_password: "400$8$45$c054050d7ce04047$335b3dec54f141e7ab030e1f...", password_salt: "CQlMGatvdGngFXiogDcW", persistence_token: "4886c517de2414c730b7ed5cc296b0e19c3e56ee398fb0aeed...", created_at: nil, updated_at: nil>
No users are created after it:
2.2.0 :005 > User.all
User Load (61.5ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
No errors and some weird SQL. What could be the problem?
EDIT: Just tried u = User.create(login:'whatever', password:'123', password_confirmation:'123'); u.errors.to_json
And I got:
=> "{\"password\":[\"is too short (minimum is 4 characters)\"],\"password_confirmation\":[\"is too short (minimum is 4 characters)\"]}"
So that was my problem, password was too short. Too bad Rails didn't print it by default when I was seeding db. Thanks all!
I have the following setup:
Schema.rb
ActiveRecord::Schema.define(version: 20130923235150) do
create_table "addresses", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "user_addresses", force: true do |t|
t.integer "user_id"
t.integer "address_id"
t.string "purpose"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
end
User.rb:
class User < ActiveRecord::Base
has_one :user_address
has_one :primary_shipping_address, through: :user_address, class_name: :UserAddress, source: :address
has_one :primary_billing_address, through: :user_address, class_name: :UserAddress, source: :address
end
Address.rb:
class Address < ActiveRecord::Base
has_one :user_address
has_one :primary_shipping_user, through: :user_address, class_name: :UserAddress, source: :user
has_one :primary_billing_user, through: :user_address, class_name: :UserAddress, source: :user
end
UserAddress.rb:
class UserAddress < ActiveRecord::Base
belongs_to :user
belongs_to :address
end
When someone does user.primary_billing_address = address, I want the join model instance to have "billing" set as its purpose. Similarly with shipping and "shipping". Ex.
irb(main):013:0> u = User.new
=> #<User id: nil, created_at: nil, updated_at: nil>
irb(main):014:0> a = Address.create
=> #<Address id: 3, created_at: "2013-09-24 00:13:07", updated_at: "2013-09-24 00:13:07">
irb(main):015:0> u.primary_billing_address = a
=> #<Address id: 3, created_at: "2013-09-24 00:13:07", updated_at: "2013-09-24 00:13:07">
irb(main):016:0> u.save!
=> true
irb(main):017:0> u.user_address
=> #<UserAddress id: 2, user_id: 3, address_id: 3, purpose: nil, created_at: "2013-09-24 00:13:18", updated_at: "2013-09-24 00:13:18">
(not what I want... purpose should be "billing")
How can I do this such that it works for new AND persisted records?. I've come up with solutions that are 90% there, but break on some random spec due to an edge case my approach didn't catch.
The trickiest part to work around is how association= behaves: on new records, it queues the association for assignment through the join model.
PS: I left out the conditionals on the has_one relationships that I'd use to get the address I want. I think this issue is independent of that.
First, the associations are a bit off, both primary_shipping_address and primary_billing_address will return same address. You can change it to
class User < ActiveRecord::Base
has_many :user_addresses # user can have multiple addresses one for shipping and one for billing
has_one :primary_shipping_address,
through: :user_address, class_name: :UserAddress,
source: :address, :conditions => ['user_addresses.purpose = ?','shipping']
has_one :primary_billing_address,
through: :user_address, class_name: :UserAddress,
source: :address, :conditions => ['user_addresses.purpose = ?','billing']
end
To save the purpose while saving the address, there are two options.
Option 1 : Override the default association= method
# alias is needed to refer to original method
alias_method :orig_primary_billing_address=, :primary_billing_address=
def primary_billing_address=(obj)
self.orig_primary_billing_address = obj
self.user_addresses.where("address_id = ?", obj.id).update_attribute(:purpose, 'billing')
end
# repeat for shipping
Option 2 : Create a custom method (I prefer this as it is cleaner and DRY)
def save_address_with_purpose(obj,purpose)
self.send("primary_#{purpose}_address=", obj)
self.user_addresses.where("address_id = ?", obj.id).update_attribute(:purpose, purpose)
end
I'm having some trouble with lazy loading, i'm pretty sure of it ... maybe you could point out to me where I've failed.
def setup_guild
if params[:guild]
#guild = Guild.where(:short_name => params[:guild].upcase).first
if #guild.nil?
puts "no guild with short name #{params[:guild]} found"
redirect_to root_path
else
#title = t "layout.guild_title", :guild_name => (#guild.name).capitalize
end
else
#guild = nil
end
end
Which is called in ApplicationController as a before filter.
At first I used Guild.find_with_short_name, but I had the same dumb answer as now ... that is :
undefined method `capitalize' for nil:NilClass
app/controllers/application_controller.rb:29:in `setup_guild'
Which is, you'd guess the #title line up there.
The thing is, if I try something similar in the console I get the expected result
ruby-1.9.2-p0 > guild = Guild.where(:short_name => "ICPT").first
Guild Load (0.5ms) SELECT "guilds".* FROM "guilds" WHERE "guilds"."short_name" = 'ICPT' LIMIT 1
=> #<Guild id: 2, name: "Inception", detail: "Inception Guild", game_server_id: 2, created_at: "2011-10-30 17:41:19", updated_at: "2011-10-30 17:41:19", short_name: "ICPT">
ruby-1.9.2-p0 > guild.name.capitalize
=> "Inception"
More, if I put something like "puts #guild.inspect" right after the fetch, the capitalization works fine, hence I think it's lazy loading failure.
I'd be happy to have some idea as to how to solve that dumb problem ... I don't really want to have an #guild.inspect for nothing in my code, i find that to be lame solution ...
Thanks !
#PanayotisMatsinopoulos As requested, here is the table Guild :
create_table "guilds", :force => true do |t|
t.string "name"
t.text "detail"
t.integer "game_server_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "short_name"
end
#PanayotisMatsinopoulos Here you go my friend ;) I still have to i18n it
#encoding: utf-8
class Guild < ActiveRecord::Base
belongs_to :game_server
has_one :game, :through => :game_server
has_many :announcement, :dependent => :destroy
validates_presence_of :name, :on => :create, :message => "dois avoir un nom"
validates_presence_of :detail, :on => :create, :message => "dois avoir une description"
validates_presence_of :game, :on => :create, :message => "dois appartenir a un jeu"
validates_presence_of :short_name, :on => :create, :message => "dois avoir un acronyme"
validates_uniqueness_of :short_name, :on => :create, :message => "est deja utilise"
validates_length_of :short_name, :within => 3..5, :on => :create, :message => "dois faire entre 3 et 5 caracteres"
validates_exclusion_of :short_name, :in => %w( users admin guilds events loots sessions characters games password), :on => :create, :message => "ne peux pas utilisé se genre d'acronyme"
validates_uniqueness_of :name, :on => :create, :message => "est deja utilise"
has_many :guild_mates, :dependent => :destroy
has_many :guild_ranks, :dependent => :destroy
has_many :guild_settings, :dependent => :destroy
has_many :events, :dependent => :destroy
has_many :characters, :dependent => :destroy
before_validation :short_name_check ,:on => :create
after_create :guild_basic_settings
def guild_basic_settings
GuildSettingType.all.each do |grst|
grs = GuildSetting.create do |g|
g.guild_id = self.id
g.guild_setting_type_id = grst.id
g.value = "false"
end
end
set_setting(["setting_allow_basic_access_for_public","setting_allow_application",
"setting_allow_event_read_for_public","setting_allow_announcement_read_for_public"],"true")
end
def set_setting(setting,value)
if setting.class == Array
setting.uniq!
setting.each do |ar|
set_setting(ar,value)
end
else
grs = nil
if setting.class == String
grs = guild_settings.includes(:guild_setting_type).where(:guild_setting_type => {:name => setting}).first
return if grs.nil?
else
grs = guild_rank_settings.where(:guild_setting_type => setting)
return if grs.nil?
end
grs.value = value
grs.save
end
end
def short_name_check
short_name.upcase! if short_name
end
def full_name
"#{name.capitalize} - #{game_server.name}"
end
def characters_for_user(user)
characters.where(:user_id => user.id)
end
def method_missing(method,*args)
check = method.to_s.split("_")
if(args.count == 0)
if check[0] == "setting"
grs = guild_settings.joins(:guild_setting_type).where(:guild_setting => { :guild_setting_types => {:name => method.to_s}}).first
unless grs.nil?
return grs.value == "true" ? true : false
else
raise "Guild > Method Missing > unknown setting : #{method.to_s}"
end
end
end
end
end
Edit : I've just seen that i didn't super method missing ... might that be the problem ?
Okay it seems that the problem was my method_missing implementation. It was lacking super call ... Now that it has been restored, everything works fine. no wonder.
Thanks #PanayotisMatsinopoulos for your help :) (also thanks a good night of sleep ;p )
you should check if name is nil also:
if #guild.nil? || #guild.name.nil?
True. method_missing should call super at the end. But, I am not convinced that your problem is there. It may be. It may not.
On the other hand, let me tell something that I believe has more chances to be your problem. This is the fact that you carry out your validation on presence of name only :on => :create. This means that an update of an object Guild that does not contain the name will pass validation and will be saved in the database without problem. Then your setup_guild will definitely throw the error:
undefined method `capitalize' for nil:NilClass
app/controllers/application_controller.rb:29:in `setup_guild'
i.e. the error this discussion started about.
Hence, my suggestion is to remove your :on => :create condition on the validation of name. (an BTW...I suggest that you remove it from all your validations unless you know what you are doing)
But then, I cannot prove that this was your problem in the first place. I am just putting here my advice, rather than my positive answer as a solution to your problem.
dear developers I have some problems with Rails models
Here is my sql tables
create_table "areas", :primary_key => "ndc", :force => true do |t|
t.string "townname", :limit => 256, :null => false
end
create_table "books", :primary_key => "ndc", :force => true do |t|
t.integer "booked", :null => false
t.integer "free", :null => false
end
class Book < ActiveRecord::Base
self.primary_key = "ndc"
has_one :area, :foreign_key => 'ndc'
end
class Area < ActiveRecord::Base
self.primary_key = "ndc"
belongs_to :book , :foreign_key => 'ndc'
end
in controller I have such code
#books = Book.paginate :page => params[:page] || 1, :per_page => 10
#books.each do |booking|
p booking.area
p booking
end
In production mode doesn't work, booking.area is nil object. what it can be ?
Area becames nil if config.cache_classes = true
so booking.area generates such queries
if cashe_classes = true
SELECT areas.* FROM areas WHERE (areas.ndc = NULL) LIMIT 1
but without cashing classes
SELECT areas.* FROM areas WHERE (areas.ndc = 30) LIMIT 1
FIXED by removing belongs_to :book , :foreign_key => 'ndc' from area class.
Your areas table needs a book_id integer field to match against the books table's primary key.
I have a rails application using the acts_as_rateable plugin.
I'm stuck on figuring out how to retrieve unrated models using this plugin - however this is more of a general rails/SQL question than specific to this plugin.
Acts as rateable adds the following to the schema:
create_table "ratings", :force => true do |t|
t.integer "rating", :default => 0
t.string "rateable_type", :limit => 15, :default => "", :null => false
t.integer "rateable_id", :default => 0, :null => false
t.integer "user_id", :default => 0, :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "ratings", ["user_id"], :name => "fk_ratings_user"
And my rated models also have a user id column.
I'd like to be able to retrieve all instances of a particular model that haven't been rated at all, and also all instances that haven't been rated by someone other than the creator of the model, e.g. here is one model:
class Review < ActiveRecord::Base
acts_as_rateable
belongs_to :user
...
end
So I want something like the following pseudocode
Review.find(:all, :conditions=>"not rated by anyone")
Review.find(:all, :conditions=>"not rated by anyone except review.user")
However I can't figure out the SQL to do this, nor the rails magic to generate that SQL :-)
Update: this query seems to find all models that ARE rated by somebody other than the user that owns the model. So I think I just need to invert this somehow.
Review.find(:all,
:joins=>'left join ratings on reviews.id=ratings.rateable_id && ratings.rateable_type="Review"',
:conditions=>'reviews.user_id <> ratings.user_id',
:group=>'reviews.id')
Named scopes are the way to go for this problem. I would add two named scopes to your Review model. Something like:
class Review < ActiveRecord::Base
acts_as_rateable
belongs_to :user
named_scope :not_rated, :conditions => { :rating => 0 }
named_scope :not_rated_by_others,
:conditions => ["user != ? AND rating == 0", self.user]
end
Then you can do:
#not_rated = Review.not_rated
#not_rated_by_others = Review.not_rated_by_others
There's a Railscast that explains named scopes.
EDIT: Second Attempt
Right, let's have another go! One of the problems is that there are multiple acts_as_rateable plugins out there. I've been testing using this one on RubyForge.
class Review < ActiveRecord::Base
belongs_to :user
acts_as_rateable
named_scope :not_rated, :select => 'reviews.*',
:joins => 'left join ratings on reviews.id = ratings.rateable_id',
:conditions => 'ratings.rateable_id is null'
named_scope :not_rated_by_others, lambda { |user| { :select => 'reviews.*',
:joins => 'left join ratings on reviews.id = ratings.rateable_id',
:conditions => ['ratings.user_id != ? and ratings.rateable_id is null',
user] }}
end
Use it like this:
frank = User.find_by_name('frank')
#not_rated = Review.not_rated
#not_rated_by_others = Review.not_rated_by_others(frank)
I've got some of the way using a query like this:
Review.find(:all,
:joins=>'left outer join ratings on reviews.id=ratings.rateable_id && ratings.rateable_type="Review"',
:conditions=>'ratings.rating is NULL')
This looks like it returns all Review models with no rating at all.
And I think this one is working to find all Review models that aren't rated by the user who created the review:
Review.find(:all,
:joins=>'left outer join ratings on reviews.id=ratings.rateable_id && ratings.rateable_type="Review" && ratings.user_id <> reviews.user_id',
:conditions=>'ratings.rating is NULL')