activerecord

How to run validations of sub-class in Single Table Inheritance?

£可爱£侵袭症+ 提交于 2019-12-10 01:14:04
问题 In my application, I have a class called Budget. The budget can be of many types.. For instance, let's say that there are two budgets: FlatRateBudget and HourlyRateBudget. Both inherit from the class Budget. This is what I get so far: class Budget < ActiveRecord::Base validates_presence_of :price end class FlatRateBudget < Budget end class HourlyRateBudget < Budget validates_presence_of :quantity end In the console, if I do: b = HourlyRateBudget.new(:price => 10) b.valid? => false b.errors

Rails 5 SQL Injection

≯℡__Kan透↙ 提交于 2019-12-10 00:53:45
问题 I've read about this for some time now on various SO threads, guides, etc... but all the answers are conflicting and contradictory. It seems there's many similar methods, and a lot of the answers say to use a different one. sanitize sanitize_conditions sanitize_sql sanitize_sql_array sanitize_sql_for_assignment sanitize_sql_for_conditions sanitize_sql_hash sanitize_sql_hash_for_assignment sanitize_sql_hash_for_conditions sanitize_sql_like I'm trying to write a 'raw query' adapter that lets me

Rails: has_many with extra details?

ぐ巨炮叔叔 提交于 2019-12-09 23:53:33
问题 While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking. A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in the database of all ingredients. That suggests two simple models: class Ingredient < ActiveRecord::Base # ingredient name, end class Recipe < ActiveRecord::Base

ruby on rails specifying uniqueness in db across multiple columns

 ̄綄美尐妖づ 提交于 2019-12-09 23:45:29
问题 I have a model as follows: class EntityTag < ActiveRecord::Base attr_protected :user_id, :post_id, :entity_id belongs_to :user belongs_to :post belongs_to :entity validates :user_id, :presence => true validates :entity_id, :presence => true validates :post_id, :presence => true end I want to guard against multiple rows which have the same combination of user_id, entity_id, and post_id (e.g. a unique ID for a row is all three of those values). What's the easiest way I can communicate that to

Best way to handle 404 in Rails3 controllers with a DataMapper get

邮差的信 提交于 2019-12-09 23:27:26
问题 It's very simple, I want to handle a normal [show] request with a call to DataMapper like I did in Merb. With ActiveRecord I could have done this: class PostsController def show @post = Post.get(params[:id]) @comments = @post.comments unless @post.nil? end end and it handles the 404 by catching the resource's exceptions. DataMapper instead doesn't do this automatically so right now I'm solving it with this solution: [moved in the answers] It is possible to tell the controller to halt inside

How to gracefully handle “Mysql2::Error: Invalid date” in ActiveRecord?

本秂侑毒 提交于 2019-12-09 21:55:04
问题 I'm building a Rails 3.2 app upon a legacy database which also has some broken records in different tables. One of the issues giving the most headache is that it includes invalid dates. I've setup a sandbox which I manually fixed one time to get my code working. Now it's time for deployment. For this reason, the sandbox is reset every night and copied from the live database, ferret indexes are rebuilt, and migrations are re-applied. We are going to deploy to the sandbox often to get in the

Selecting posts with multiple tags

自闭症网瘾萝莉.ら 提交于 2019-12-09 21:23:20
问题 I'm implementing a tagging system on a blog app. This app has posts, posts have many tags through taggings. More or less like RailCasts #382 http://railscasts.com/episodes/382-tagging I will use checkboxes to select posts with multiple tags like this: Post.joins(:tags).where(:tags => { :id => [tag_ids] } ) But what if I want to join posts that have all the required tags instead of posts that meet only one requirements? For exapmle: Post1 has tags "foo, bar, baz" Post2 has tags "bar, baz"

Is that Possible to make use of 'activerecord-import' gem for a Model-less table in Rails.?

為{幸葍}努か 提交于 2019-12-09 19:42:28
问题 I want to bulk insert records into a table, which does not have a Model. I did follow the link How to implement bulk insert in Rails 3 .. Everything was fine except the 'import' command. Because I do not have a Model. I can not create an empty Model for that table. Okay, I tell you, why I cant create a table for that. I am using IOS apns server for Push Notification feature. When I configured that, it created lot of tables into my database without Models. In one of these tables I want to bulk

Rails: How to implement counter caching with self-referential Many to Many via has_many :through

ε祈祈猫儿з 提交于 2019-12-09 18:42:25
问题 How can I roll my own counter cache for a self-referential many-to-many relationship that uses has_many :through ? I need to track the number of citations and references for each article I'm using roughly the code from the answer to this question: class Publication < ActiveRecord::Base has_many :citations has_many :cited_publications, :through => :citations, :source => :reference has_many :references, :foreign_key => "reference_id", :class_name => "Citation" has_many :refered_publications,

Rails “where” clause for associations

孤街醉人 提交于 2019-12-09 18:33:42
问题 This seems like a simple question but it's a little puzzle to me: class Parent has_many children ... end class Child belongs_to parent end p = Parent.find(111) c = Child.all.where(parent: p) Why doesn't that work, and how come I have to do: c = Child.all.where(parent_id: p.id) Thanks! * Addendum * A more complicated case has me creating a Relation based on more complicated logic, e.g. c = Child.where(age: 32, city: "boston") c.where(parent: p) # wouldn't work * Addendum #2 * Wait I need to