Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can models reference the default value in a schema when they are created using the new keyword? #912

Closed
CrowdHailer opened this issue Nov 11, 2014 · 4 comments

Comments

@CrowdHailer
Copy link

This is a migration from the project I am currently working on

Sequel.migration do
  up do
    alter_table :articles do
        add_column :published?, TrueClass, default: false
    end
  end

  down do
    alter_table :articles do
        drop_column :published?
    end
  end
end

I want to get the same behavior for articles created new as for ones pulled from the database

article = Article.new
article.published? #=> nil  I expect this to return false

article = Article.create
article.published? #=> false

Admittedly in this case false vs nil is not a deal breaker but would be a very different situation for nearly any other default case

@CrowdHailer
Copy link
Author

I have been going through the plugins and found
http://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/DefaultsSetter.html

These seams to describe what I want but I can't get it to work. I am using padrino and have added the plugin with the other plugin declarations

/config/database.rb

Sequel::Model.plugin(:schema)
Sequel::Model.plugin :defaults_setter
Sequel::Model.raise_on_save_failure = false # Do not throw exceptions on failure
Sequel::Model.db = case Padrino.env
  when :development then Sequel.connect("postgres://localhost/commentr_development", :loggers => [logger])
  when :production  then Sequel.connect("postgres://localhost/commentr_production",  :loggers => [logger])
  when :test        then Sequel.connect("postgres://localhost/commentr_test",        :loggers => [logger])
end
Sequel::Model.db.extension :pg_array

@jeremyevans
Copy link
Owner

You don't describe how it isn't working for you. When submitting a bug report, you should provide a self-contained, reproducible example showing the problem.

Using your example, it seems to be fine here:

$ bin/sequel -E sqlite:/ 
I, [2014-11-11T10:36:42.209794 #16048]  INFO -- : (0.002182s) PRAGMA foreign_keys = 1
I, [2014-11-11T10:36:42.210117 #16048]  INFO -- : (0.000085s) PRAGMA case_sensitive_like = 1
Your database is stored in DB...
irb(main):001:0> DB.create_table(:articles){TrueClass :published?, :default=>false}
I, [2014-11-11T10:36:46.054718 #16048]  INFO -- : (0.001044s) CREATE TABLE `articles` (`published?` boolean DEFAULT (0))
=> nil
irb(main):002:0> class Article < Sequel::Model; end
I, [2014-11-11T10:36:52.219446 #16048]  INFO -- : (0.001404s) PRAGMA table_info('articles')
=> nil
irb(main):003:0> Article.plugin :defaults_setter
=> {:published?=>false}
irb(main):004:0> Article.new.published?
=> false

Note that as the contributions guidelines state, GitHub Issues should not be used as a help form, only for reporting bugs. Please use IRC or the sequel-talk Google Group if you need help using Sequel.

@CrowdHailer
Copy link
Author

Thanks for the input. I will look through as to where the error has come from.

I will use one of the mentioned channels in future. As a further note. The documentation to this project is some of the best I have found. However its comprehensive nature is quite intimidating to a reasonably recent newcomer. Even the list of further resources on the README suggests five more places to get help. Google group doesn't stand out as the place to get help unless you know that and I see no mention of IRC. perhaps a section along the line of 'Questions? please use the refer to the google group' Because some repos have quite active discussions in their issue section.

@jeremyevans
Copy link
Owner

I think you are right, it makes sense to make things more explicit in the README, since that is the first place people are going to look. I'll take care of that.

jeremyevans added a commit that referenced this issue Nov 11, 2014
This was mentioned in a few other places, but it makes sense to
have it in the README.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants