activerecord

Why does rails not respect the type of a belongs_to associated object with STI, when its superclass is abstract?

房东的猫 提交于 2019-12-10 16:03:17
问题 I've come across this rather odd bit of behaviour in a rails application I'm working on. I have multiple types of Post in an inheritance heirarchy, and a Post has_many FeedEntries. class Post < ActiveRecord::Base has_many :feed_entries end class Post::BlogPost < Post; end class Post::Discussion < Post; end class Post::Article < Post; end class FeedEntry < ActiveRecord::Base belongs_to :post end Now, when I have everything set up as before, calling FeedEntry#post on a saved object always

Using a Decorator, (rails) Could not infer a decorator for ActiveRecord::Base

牧云@^-^@ 提交于 2019-12-10 15:55:14
问题 I'm having trouble using a decorator. I've never used one before and I've been trying to use one with regards to something that I've been doing for breaking up some emails. However because I've never used one before, I've been having trouble even doing very simple things with my decorator and I'm thinking there is some form of setup issue with it. I do know that everything outside of my little feature (aka the gemfile and such) are all up to date and proper. The error I am getting is simply,

CodeIgniter Active Record For Joining The Tables

非 Y 不嫁゛ 提交于 2019-12-10 15:44:22
问题 I've following tables. apartments id name slug created modified apartment_amenities id name slug apartment_id created modified apartment_activities id name slug apartment_id created modified In the view I wanted something like this. no apartment_name amenities activities 1 shobha_comnplex party hall pamplets swimming pool banners play area boards 2 navami_comnplex party hall boards swimming pool banners club house pamplets In the model I tried like this. $this->db->select('apartments.id,

Rails 3.1 named_scope

柔情痞子 提交于 2019-12-10 15:39:33
问题 What is the Rails 3.1 of writing the code below: named_scope :min_2_items_last_90_days, { :include => { :orders => :order_items }, :conditions => ['orders.created_at >= ?', 90.days.ago], :group => 'people.id', :having => 'COUNT(order_items.id) >= 2' } 回答1: While writing it as scope :min_2_items_last_90_days, where(...) is syntactically correct, it probably (like your original code) doesn't do quite what you think. In both cases the 90.days.ago is evaluated once only, when the class is loaded,

decorating an attribute in rails

限于喜欢 提交于 2019-12-10 15:32:51
问题 I have a name attribute on a Person model and every time I access the name attribute, I want name.capitalize to be returned. Doing the following inside the model won't work, def name name.capitalize end so what is the alternative? 回答1: I suggest you to create a secondary method with your custom formatters. class Person def formatted_name name.capitalize end end This is a better solution compared with overwriting the default implementation because setter and getters might called when updating

where does database.yml get loaded in ActiveRecord in Rails 4?

狂风中的少年 提交于 2019-12-10 15:25:36
问题 What line (or method) in the ActiveRecord codebase does the config/database.yml for a Rails application get loaded? (I'm looking at 4.0.5 specifically, but if anyone has any information on >=4.0.5, that would be illuminating)? 回答1: It's inside the Railties, specifically in the file railties/lib/rails/application/configuration.rb in lines 101–116 (for Rails 4.0.5): https://github.com/rails/rails/blob/v4.0.5/railties/lib/rails/application/configuration.rb#L101-L116 # Loads and returns the

Rails ActiveRecord: Pluck from multiple tables with same column name

痴心易碎 提交于 2019-12-10 15:09:44
问题 I have a simple use case: User has many Tweets Tweet belongs to User And i'm trying to pluck a column name that exists on both tables. For example: @tweets = Tweet.includes(:user).all.pluck(:created_at) Each table has a created_at column, but the result from above returns the created_at for the tweet. How can I also pluck the user's created_at? My workaround is below utilizing joins and selects: @tweets = Tweet.joins(:user).select("users.created_at AS created_date").all So how can I do this

Models not reloading in development in Rails (3.2.11) project

谁说胖子不能爱 提交于 2019-12-10 14:56:13
问题 I've searched fairly extensively for any advice and have yet to find it so, here goes: My Rails project fails to automatically reload models in development. Reloading them currently requires a full server restart. Previous instances of this issue have been related to non-activerecord files placed in the models directory, though this is not the case for me. config.cache_classes is properly set to false in my development config file. Views and controllers reload without issue. All of my rails

.where vs find. ActiveRecord::Relation NoMethodError

↘锁芯ラ 提交于 2019-12-10 14:53:13
问题 I'm new to rails and this might seem obvious, but couldn't find a answer. when i do u = User.where("email=?", email_string) u.name = "new name" doesn't work i keep getting NoMethodError: undefined method `name=' for #<ActiveRecord::Relation:0x1049c2890> but if i change u = User.where("email=?", email_string) to u = User.find_by_email(email_string) i can see my changes being persisted and no error thrown. So what am i missing. is it that .where returns a read only object or something ? 回答1:

Behind the scenes: How does an ORM “think”?

假如想象 提交于 2019-12-10 14:45:53
问题 I'm interested in some of the design behind Rails ActiveRecord, Doctrine for PHP (and similar ORMs). How does an ORM manage to accomplish features like chained accessors and how deep are they typically expected to work? How does an ORM construct queries internally? How does an ORM manage the queries while sustaining the arbitrary nature of all that is expected of it? Obviously this is an academic question, but all natures of answers are welcome! (My language of choice is OO PHP5.3!) 回答1: