activerecord

PHP: Active record table joins

蓝咒 提交于 2019-12-13 02:38:45
问题 I have an app that uses the codeigniter CXTags tagging library. The database structure is as follows: posts id tags_ref row_id table tag_id tags id safe_tag tag My query basically goes if $safe_tag is not null then join tags_ref on post.id = tags_ref.row_id, join tags on tags_ref.tag_id = tags.id, where tags_ref.table = 'posts' and tags.safe_tag = 'food' SELECT * FROM posts JOIN tags_ref ON posts.id = tags_ref.row_id JOIN tags ON tags_ref.tag_id = tags.id WHERE tags.safe_tag = $safe_id

optimize sql query rails

自闭症网瘾萝莉.ら 提交于 2019-12-13 02:27:20
问题 On posts index page I list all posts this way: posts_controller.rb def index @posts = Post.includes(:comments).paginate(:page => params[:page]).order("created_at DESC") end index.html.erb <%= render @posts %> _post.html.erb <%= gravatar_for post.user, size:20 %> <%= link_to "#{post.title}", post_path(post) %> <%= time_ago_in_words(post.created_at) %> <%= post.comments.count %> <%= post.category.name if post.category %> 35 posts per page When I first load the page in dev env, rack-mini

Can I hook into ActiveRecord connection establishment?

我与影子孤独终老i 提交于 2019-12-13 02:22:49
问题 I would like to add a user-defined function using Sqlite3's create_function, which will be used by database triggers. Is there a way to hook into ActiveRecord connection establishment to run some code each time a connection to the database is made, where one could create the function and make it available to the triggers? This would also be useful for setting pragmas on the connection. 回答1: Here is what I've found on my own. Using an initializer in app/config/initializers I do this:

Build a table which has a Field of User_ids, and then querying for a particular User_Id

谁说胖子不能爱 提交于 2019-12-13 01:57:55
问题 I need some help building a table and then getting data from that table in Rails 3. Here's the break down: Models - 3 models involved here they are: Thread has many participants Participants belong to thread Users Activity table: id | thread_id | participants Example records would look something like: 1 | 300 | 3,1,5,67,13 2 | 333 | 3,12 3 | 433 | 1,12 4 | 553 | 1,12, 67 Where participants, is a list of user_ids, if there is a better way to store the user_ids please let me know. I haven't

undefined method `type_cast' for #<ActiveRecord::ConnectionAdapters::Column> (NoMethodError)

折月煮酒 提交于 2019-12-13 01:43:53
问题 ActiveRecord::ConnectionAdapters::Column used to have a method called type_cast which took a string and cast it "to an appropriate instance". This appears to have been removed at some point, and I can't figure out what I should do to replace it. Here is the code that uses it: # Create a column that will be responsible for typecasting @column = ActiveRecord::ConnectionAdapters::Column.new(attribute.to_s, options[:default], @type == 'any' ? nil : @type) # Typecasts the value based on the type

ActiveRecord, double belongs_to

房东的猫 提交于 2019-12-13 01:35:26
问题 I have 2 models: Link and User such as: class Link < ActiveRecord::Base belongs_to :src_user belongs_to :dst_user end class User < ActiveRecord::Base has_many :links end A schema could looking like: +----------+ +------+ | Link | | User | +----------+ |------+ | src_user |---->| | | dst_user |---->| | +----------+ +------+ My question is: how can I edit User model do in order to do this @user.links # => [list of links] (...which should query @user.src_users + @users.dst_users, with unicity if

How to update deeply nested models that accept nested attributes?

别等时光非礼了梦想. 提交于 2019-12-13 01:29:44
问题 I have a problem mostly identical to this one. I have three models call them Parent, Child, Grandchild. Parent has_many :children has_many :grandchildren, through: children When creating a parent, I assign children to it via collection check boxes. Now I need to update grandchildren association on the parent form. I set up accepts_nested_attributes_for :children and I am able to update the attributes just like I need to. The problem arises when I attempt to update a parent record. If I remove

How to add a Hash object to an ActiveRecord class? Tried but migration fails

…衆ロ難τιáo~ 提交于 2019-12-13 01:25:53
问题 I want my ActiveRecord class User to contain options (a bunch of string key-values), so I wrote: rails generate migration AddOptionsToUser options:Hash It generated: class AddOptionsToUser < ActiveRecord::Migration def self.up add_column :users, :options, :Hash end def self.down remove_column :users, :options end end I also added this line to my class User : serialize :options, Hash But the migration fails: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds

I have different results from query for COUNT('e.id') or COUNT(e.id)

不打扰是莪最后的温柔 提交于 2019-12-13 01:19:44
问题 I have following code: def self.department_members(department) where(organization_id: department.organization_id) .joins("LEFT JOIN core_employments As e ON e.organization_id = #{department.organization_id} AND core_members.user_id = e.user_id") .group('core_members.id') end def self.can_automerged(department) department_members(department).having("COUNT('e.id') = 1") # department_members(department).having("COUNT(e.id) = 1") end def self.can_not_automerged(department) department_members

codeigniter active record, when to escape, if at all

a 夏天 提交于 2019-12-13 00:53:34
问题 I just recently started using active record (before I just wrote manual queries since I was so used to them). I was looking at the code of ion_auth and I saw that in a few places the strings had been escaped even though active record was used, i.e ->where($this->identity_column, $this->db->escape_str($identity)) ->where($this->tables['groups'].'.name', $this->db->escape_str($group)) Thing is, I havent escaped anywhere where I have used active record since on the documentation it said active