activerecord

rails // query by timezone during daytime

烂漫一生 提交于 2020-01-02 05:26:11
问题 Not that confident when it comes to use timezones, looking for some help on this one I have a cron job that check users every once in a while, which call a rake task. Within this rake task, I query users and sent each an email base on conditions. I have the timezone info for each users. I'd like to have a query on those users that would only return the users that are currently in their daytime How would one achieve that ? Best 回答1: This depends on a few different things, but I'll go by how my

How to update a model's “updated_at” field only for a subset of column updates?

百般思念 提交于 2020-01-02 05:24:06
问题 There is a typical blog application. Each user has_many posts. Each post has_many tags. I'm sorting each post by updated_at so the most recently updated post will show up on top. So for example, if I update a post's content, the post will come up to the top. However, this also happens when I just add a tag, since a tag is connected to its corresponding post. I only want the content update to change updated_at field. I don't want updated_at for a post to be changed because I added a tag. Is

How does has_one :through work?

旧巷老猫 提交于 2020-01-02 04:52:06
问题 I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through => :pack_release_items end class Pack < ActiveRecord::Base has_many :pack_release_items has_many :release_items, :through=>:pack_release_items end class PackReleaseItem < ActiveRecord::Base belongs_to :pack belongs_to :release_item end The problem is that, during execution, if I add a pack to a release_item it is not aware that the pack is a pack. For instance: Loading development

ActiveRecord returns data in ASCII-8Bit Under Ruby 1.9.2-rc1

ⅰ亾dé卋堺 提交于 2020-01-02 04:11:15
问题 Further to the title, when loading data from ActiveRecord the encoding is always set to ASCII-8Bit in spite of my best efforts to force the encoding. I have entered as much detail as possible here to try and build a good error report someone could use to help me out! The project is using the following technologies: Padrino Framework Ruby 1.9.2-rc2 (Also 1.9.1 and 1.9.2-preview3) ActiveRecord MySQL (Full List) $ bundle show | ack '(record|padrino)' * activerecord (2.3.8) * padrino (0.9.14) *

Matching nested model association attribute with includes

…衆ロ難τιáo~ 提交于 2020-01-02 04:08:31
问题 Suppose I have the following models: class Post < ActiveRecord::Base has_many :authors class Author < ActiveRecord::Base belongs_to :post And suppose the Author model has an attribute, name . I want to search for all posts with a given author "alice", by that author's name. Say there is another author "bob" who co-authored a post with alice. If I search for the first result using includes and where : post = Post.includes(:authors).where("authors.name" => "alice").first You'll see that the

How can I access a Postgres column default value using ActiveRecord?

天涯浪子 提交于 2020-01-02 04:06:04
问题 I'm trying to access a column's default value in my Postgres 9.2 database. By using raw SQL, I can verify that the column default is "users_next_id()": > db = ActiveRecord::Base.connection > db.execute("SELECT table_name,column_name,column_default FROM information_schema.columns WHERE table_name = 'users' and column_name ='id'").first => {"table_name"=>"users", "column_name"=>"id", "column_default"=>"users_next_id()"} But when I use AR's 'columns' method, the default value appears to be nil:

Make a one-off query to a different database and table

≡放荡痞女 提交于 2020-01-02 03:33:13
问题 I have a rails app with a wordpress blog sellotaped on the side (totally separately at /blog). The client want's to the latest blog post on the main homepage of the rails app, so I need to do a one-off mysql query to the word-press database. How would I go about doing this in the rails app. The word-press is completely sperate from rails in terms of database. Cheers. 回答1: Assuming it is accessible using the same database credentials and on the same MySQL server, the easiest way would be to

CodeIgniter Active Record vs. regular queries

99封情书 提交于 2020-01-02 02:33:07
问题 Currently I use CodeIgniter with regular queries, i.e.: $sql = " SELECT * FROM my_table WHERE item_id > 1 "; $q = $this->db->query($sql); I have started looking into ActiveRecord, and it does look nice, and has the advantage of the query being built regardless of which database driver is used - however, I strictly use a single database type per project in general, so this is not really a benefit for me. I found that regular queries (as I have in my example) are more readable and easier

Multiple database tables within one AR model in Rails 3

会有一股神秘感。 提交于 2020-01-02 02:01:10
问题 I was asked to make some kind of reporting (logging) service. The employee has locally installed web application (just some dynamic website, written in PHP) in many companies. This web app is some kind of survey. All data is saved on local database, but now the requirement is that this data (result of survey) will also be sent to central server after every form submit. There are four types of surveys. They have organised it this way, that there are many Projects, and each Project can have

Rails Filter records of child model based upon the parent model attribute

谁都会走 提交于 2020-01-01 19:47:47
问题 Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? 回答1: Just add this to your Upload model: named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL" then you can get all the uploads