activerecord

Check if current_user is the owner of a resource and allow edit/delete actions

你。 提交于 2019-12-20 09:38:32
问题 Example: User A (id=10) has created a photo resource photo: (id: 1 user_id = 10, url: "http://...") Now, if User B (id=20) go to this url: /photos/1/edit it can edit photo of user A !!! Rails+Devise provides something for this by default? It seems it's a very common issue I just need to allow that any user can edit/delete ONLY resource it has created (where current_user == resource.user) Using: Rails 4, Devise Update: I think CanCan it's something too advanced. I don't need roles or restrict

Rails Scope returns all instead of nil

一笑奈何 提交于 2019-12-20 08:57:02
问题 I'm running into a strange issue creating a scope and using the first finder. It seems as though using first as part of the query in a scope will make it return all results if no results are found. If any results are found, it will correctly return the first result. I have setup a very simple test to demonstrate this: class Activity::MediaGroup < ActiveRecord::Base scope :test_fail, -> { where('1 = 0').first } scope :test_pass, -> { where('1 = 1').first } end Note for this test, I have set

How do I turn on SQL debug logging for ActiveRecord in RSpec tests?

穿精又带淫゛_ 提交于 2019-12-20 08:09:12
问题 I have some RSpec tests for my models and I would like to turn on SQL ActiveRecord logging just like I see in the Rails server mode. How to do that? I start my tests with RAILS_ENV=test bundle exec rspec my/test_spec.rb Thanks 回答1: By default, all your db queries will be logged already in test mode. They'll be in log/test.log . 回答2: You could try setting the ActiveRecord logger to stdout in your test somewhere. If you're using rspec, maybe in the spec helper? ActiveRecord::Base.logger =

How do you scope ActiveRecord associations in Rails 3?

风格不统一 提交于 2019-12-20 08:01:34
问题 I have a Rails 3 project. With Rails 3 came Arel and the ability to reuse one scope to build another. I am wondering if there is a way to use scopes when defining a relationship (e.g. a "has_many"). I have records which have permission columns. I would like to build a default_scope that takes my permission columns into consideration so that records (even those accessed through a relationship) are filtered. Presently, in Rails 3, default_scope (including patches I've found) don't provide a

Why all the Active Record hate? [closed]

巧了我就是萌 提交于 2019-12-20 07:58:31
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . As I learn more and more about OOP, and start to implement various design patterns, I keep coming back to cases where people are

Bug in implemented tagging system

喜欢而已 提交于 2019-12-20 07:38:02
问题 I have followed Hartl's tutorial to make a ToDoList with a tagging system, also with the help of this word guide and video. However, I do not really understand how the tagging system works in terms of the filtering, so I just copied it and improvised. Now I have a problem with my filtering system as now it just redirects to a brand new page that contains all tasks including those that doesn't have the clicked tag. Before After Here is the updated log on my command line after inserting puts

Rails can't write to serialized array field in database

时光总嘲笑我的痴心妄想 提交于 2019-12-20 07:14:05
问题 I have the array on my model and set up, and when I go to write the array onto my record it says COMMIT true, yet checking the field on that record immediately after returns an empty array. Model: class Feature < ActiveRecord::Base serialize :content, Array attr_accessible :content end Migration: class AddContentToFeatures < ActiveRecord::Migration def change add_column :features, :content, :text, array: true, default: [] end end What I tried, along with various symbol and string syntaxes, is

Devise `find_first_by_auth_conditions` method explanation

非 Y 不嫁゛ 提交于 2019-12-20 07:12:48
问题 My two methods using Devise: Method1 def self.find_first_by_auth_conditions(warden_conditions) conditions = warden_conditions.dup where(conditions).where(["lower(username) = :value OR lower(email) = :value", {:value => signin.downcase }]).first end Method2 def self.find_for_database_authentication(warden_conditions) conditions = warden_conditions.dup login = conditions.delete(:signin) where(conditions).where(["lower(username) = :value OR lower(email) = :value", {:value => login.strip.downcase

Many-to-Many-to-Many relationship with need for another specific model

不打扰是莪最后的温柔 提交于 2019-12-20 06:41:20
问题 I have a many-to-many relationship between Supermarket , Product and Brand through the Supply - and Origin -models. I also want to store which specific Product-Brand-Combination I have in my supermarket. I thought of another model (I called it Specific_Combination where I would store :supermarket_id , :product_id and :brand_id . class Supermarket < ActiveRecord::Base has_many :supplies has_many :products, :through => :supplies end class Supply < ActiveRecord::Base belongs_to :product belongs

Updating and delete Related Models (Relational tables) in Yii

a 夏天 提交于 2019-12-20 06:08:52
问题 UPDATED I have two related models i.e. Candidate and Qualifications. They have one to one relationship between them. I am using CActiveForm and want to perform CRUD operation on the relational data. I had been able to insert the data but i am having issues with the update and delete. The function is displaying the $id but that the only thing that is displayed on the page. CandidateController public function actionUpdate($id) { echo $id;// this is printing the id on page $model = Candidate: