activerecord

Rails 3.1 distinct find and missing attributes

风流意气都作罢 提交于 2019-12-07 09:16:18
问题 class State < ActiveRecord::Base has_many :cities end class City < ActiveRecord::Base belongs_to :state has_many :companies end class Company < ActiveRecord::Base belongs_to :city end I'm trying to list all states, and their respective cities, that contain at least one company registered. My first try was the following query: states = State.joins(:cities => :companies).includes(:cities) Which works, but I end up getting duplicates if a state has more than one city with companies in it. I then

Disable BLOB logging in Rails 3

China☆狼群 提交于 2019-12-07 08:44:45
问题 Is there a way to disable/truncate BLOB fields in the logged SQL queries? When I insert or update a record with BLOB fields in it Rails logger prints the contents of these fields which is very annoying. I found some solutions but none of them work with Rails 3. 回答1: I think there are a couple of things you can do, one would be to override the Logger format_message function and remove BLOB fields from the logger message: class Logger def remove_blobs msg ... end def format_message(severity,

Is it possible to UPDATE a JOINed table using Codeigniter's Active Record?

梦想与她 提交于 2019-12-07 08:39:29
问题 Here is what I'd like to do function edit_save($data, $post_id, $user_id) { $this->db->where('post.user_id', $user_id); $this->db->where('post.post_id', $post_id); $this->db->join('data', 'post.data_id_fk = data.data_id', 'left'); $this->db->update('post', $data); } The 'post' table needs to be left-joined with 'data'. When I run the above I get a SQL error saying that one of the fields from the 'data' table is not found. Any suggestions? MORE INFO This is the generated SQL query UPDATE `post

Validate presence of nested attributes

怎甘沉沦 提交于 2019-12-07 08:35:47
问题 How do I validate that a model has at least one associated model using nested attributes? This has been driving me crazy as I am sure that I am missing something simple. For example, I want to require that a List always has at least one Task. class List < ActiveRecord::Base has_many :tasks, :dependent => :destroy accepts_nested_attributes_for :tasks, :allow_destroy => true end class Task < ActiveRecord::Base belongs_to :list end I've tried many different options. 1- adding a validation to

Best practices for getting a list of IDs from an ActiveRecord model

拈花ヽ惹草 提交于 2019-12-07 08:21:51
问题 I have an ActiveRecord model Language , with columns id and short_code (there are other columns, but they are not relevant to this question). I want to create a method that will be given a list of short codes, and return a list of IDs. I do not care about associations, I just need to end up with an array that looks like [1, 2, 3, ...]. My first thought was to do something like def get_ids_from_short_codes(*short_codes) Language.find_all_by_short_code(short_codes.flatten, :select => 'id').map(

Rails activerecord : sum, max and joins

淺唱寂寞╮ 提交于 2019-12-07 08:19:11
问题 I have two models users and posts . An user can votes and views a post #users id name #posts id count_votes count_views users_id created_at updated_at I want the user who received the most votes and views on his posts from the last 24 hours. The biggest sum of views and votes win. WHAT I TRIED I have this SQL query, it's good but I would like to have the user with the max of votes, this one give me all users and I don't know how to add count_views select u.name as "Name", sum(p.count_votes)

Automatically change status of post Rails

孤人 提交于 2019-12-07 08:14:14
问题 In my rails application I have a job model which I want the status to be changed automatically to "archived" after 30 days from approval by the admin is this possible? if so what is the way to do it? 回答1: I would add an attribute named "archive_time" as a datetime when it enters the approved state. Then you can set up a rake task to set the archived state and where the archive_time is in the past. This might look like this: jobs = Job.where("state = ? and archive_time >= ?", 'approved', Time

Access SQL computed columns through ActiveRecord

痴心易碎 提交于 2019-12-07 08:10:41
问题 I have a Person model, which includes a property representing the data of birth ( birth_date ). I also have a method called age() , which works out the current age of the person. I now have need to run queries based on the person's age, so I have replicated the logic of age() as a computed column in MySQL. I cannot workout how I would make this additional column part of the default select statement of the model. I would like to be able to access the age as if it were a native property of the

Rails where clause when something is stored as array

家住魔仙堡 提交于 2019-12-07 08:10:35
问题 I am running rails 4.2, with a PG database. I have an item stored in the database such as (model Item ): :something => ["1", "2", "3"] I would like to get the Item.where(:something.include? => "3") Obviously this is not working - but how are you meant to do this in rails? 回答1: According to documentation, something like this should work: Item.where('something @> ARRAY[?]::varchar[]', ['3']) 回答2: In addition to @potashin answer, there is a shorter way to do (see documentation) if you need to

Rails 3 and Rspec: counter cache column being updated to 2 when expected 1

☆樱花仙子☆ 提交于 2019-12-07 07:21:11
问题 I'm testing with Rspec a model named Solutions which has many Likes. Solution stores how many Likes it have (counter_cache). It has a "likes_count" attribute (and respective db field). When I create a Like record associated to a Solution, I expect that the solution attribute "likes_count" should be updated from nil to 1. When I do that in console, it works. But when I run the spec, doing the SAME THING I do in console, it update TWICE the "likes_count" field, setting it to 2. Take a look (in