activerecord

Reset the id field in a table in a sqlite db in Rails

时光毁灭记忆、已成空白 提交于 2021-02-20 04:59:07
问题 Im using sqlite db in a sample rails application. From my users table, i have cleared all records using User.delete_all . But now whenever i insert a new record in the table using User.create , the id is starting at a value which is one more than the id of the last record which was there in the table. For example, if my table had 5 records and i cleared all, then when i do User.create, its starting at id 6. Is there any way i can make the id start from 1 again ? Thank You 回答1: Similar

Rails sees mysql tinyint(1) as a boolean - but I want it to be a number

我与影子孤独终老i 提交于 2021-02-19 03:43:09
问题 Rails 4.2.1 using mysql2 gem. ActiveRecord treats a mysql column with data type tinyint(1) as a boolean. But I want to use it as a small number - I want to store values up to 100 which is ok for tinyint(1) . When I try to create a record, the tinyint column casts to false and I get a depreciation warning: > Foo.create(my_tinyint_col: 13) (0.2ms) BEGIN SQL (0.5ms) INSERT INTO `foos` (`my_tinyint_col`) VALUES (0) (107.3ms) COMMIT => #<Foo ID: 519, my_tinyint_col: false> DEPRECATION WARNING: You

counter_cache not decrementing for has_many associations in ActiveReord

非 Y 不嫁゛ 提交于 2021-02-19 03:43:09
问题 My Rails 3 app has 2 models and a third that's join table between them and their has_many relationships. Basically, User and Show are joined by SavedShow, allowing users to save a list of shows: class Show < ActiveRecord::Base has_many :saved_shows has_many :users, :through => :saved_shows end class User < ActiveRecord::Base has_many :saved_shows has_many :shows, :through => :saved_shows end class SavedShow < ActiveRecord::Base belongs_to :user, :counter_cache => :saved_shows_count belongs_to

Rails 3 has_many changed?

可紊 提交于 2021-02-18 05:31:26
问题 I need to track changes (additions and deletes) of an association set up like this: has_many :listing_services has_many :services, through: :listing_services For normal attributes the easist way to do it is to check l.changes[attribute] in before_save or l.previous_changes[attribute] in after_save . The question is, what is the best way to do it for has_many attributes? 回答1: I didn't use changes method. But I'm sure that you always can use magic methods <attribute_name>_changed? and

Rails - only find records where has_many associated records are present

核能气质少年 提交于 2021-02-17 19:21:26
问题 I have a model with a has_many relation with another model like so: class Parent < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base belongs_to :parent end Since there are some parents that may not have children, I'd like to do a query that returns only the parents that DO have children. How would I go about something like this? Since I'm doing this on Rails 3, it would be helpful if this query were to not use the where.not. syntax. 回答1: Rails 4 Parent.includes(:child

Rails first_or_create ActiveRecord method

我怕爱的太早我们不能终老 提交于 2021-02-17 18:46:21
问题 What does the first_or_create / first_or_create! method do in Rails? According to the documentation, the method " has no description "... 回答1: From the Guides first_or_create The first_or_create method checks whether first returns nil or not. If it does return nil, then create is called. This is very powerful when coupled with the where method. Let’s see an example. Suppose you want to find a client named ‘Andy’, and if there’s none, create one and additionally set his locked attribute to

Rails first_or_create ActiveRecord method

做~自己de王妃 提交于 2021-02-17 18:40:30
问题 What does the first_or_create / first_or_create! method do in Rails? According to the documentation, the method " has no description "... 回答1: From the Guides first_or_create The first_or_create method checks whether first returns nil or not. If it does return nil, then create is called. This is very powerful when coupled with the where method. Let’s see an example. Suppose you want to find a client named ‘Andy’, and if there’s none, create one and additionally set his locked attribute to

virtual attribute in a query

早过忘川 提交于 2021-02-11 01:56:07
问题 User model has all_scores attribute and i created the method below models/user.rb def score ActiveSupport::JSON.decode(self.all_scores)["local"] end What i'm trying to do this using this virtual attribute score to filter users. For example: I need the users whose score is over 50. The problem is i can't use virtual attribute as a regular attribute in a query. User.where("score > 50") # i can't do this. Any help will be appreciated. 回答1: Well, the "easiest" solution would probably be User.all

virtual attribute in a query

泪湿孤枕 提交于 2021-02-11 01:55:16
问题 User model has all_scores attribute and i created the method below models/user.rb def score ActiveSupport::JSON.decode(self.all_scores)["local"] end What i'm trying to do this using this virtual attribute score to filter users. For example: I need the users whose score is over 50. The problem is i can't use virtual attribute as a regular attribute in a query. User.where("score > 50") # i can't do this. Any help will be appreciated. 回答1: Well, the "easiest" solution would probably be User.all

ActiveRecord SQL queries not getting logged

旧巷老猫 提交于 2021-02-10 12:01:21
问题 I have been facing a weird problem for some time, wherein ActiveRecord queries are not getting logged in the terminal, or in the Rails console (using ActiveRecord::Base.logger = Logger.new(STDOUT) ). This is the exception that I get: Could not log "sql.active_record" event. NameError: undefined local variable or method `s' for # ActiveSupport::Notifications::Event:0x007f9ae02a60c0. I tried out a few things, including reinstalling Rails, but to no avail. Apart from wondering why this is