activerecord

handling order with has_many through relationship

徘徊边缘 提交于 2019-12-09 16:24:18
问题 I have two models: project and task (for example) with a join model: project_task enabling a has_many through relationship so that tasks may be shared across projects. I have specified position as an attribute of the project_task model. Now I want to be able to access tasks by their position in the project_tasks table via a given project. i.e. project.tasks (ordered by the position listed for each task in the project_tasks table). Is this possible? 回答1: I think something like that can help

Rails - Including associations with dynamic conditions

浪子不回头ぞ 提交于 2019-12-09 16:15:54
问题 Given a school model and a student model with the school having a has_many relation to student: has_many :students, :conditions => proc { "year_id=#{send(:active_year_id)}" } where active_year_id is a method defined in the school model, I'm encountering an error that "active_year_id is undefined" when calling: School.where(:active => true).includes(:students) The condition works fine when I do, say, School.where(:id => 10).students Only when I try to use includes do I get that error. Is that

How to do has_many with a model that is in a namespace

风流意气都作罢 提交于 2019-12-09 14:54:57
问题 Rails 3 gave us these nice generators that place models and controllers into namespaces, whoo! But now I'm unable to associate them with other models. I'm trying to achieve a has_many through association with namespaced models. I've been searching around the internet for a few days and I haven't come up with any examples. /app/models/templates/practice.rb class Templates::Practice < ActiveRecord::Base has_many :practice_sequences, :order => "position", :dependent => :destroy, :class =>

How to fetch title of an item from a database and send it to the header template in CodeIgniter

喜夏-厌秋 提交于 2019-12-09 14:05:58
问题 I am writing an application in CodeIgniter where I specify the <title> meta-tag on every page in every controller which I have managed to send to my header template. However, now I have created an application that fetch credit cards and their titles from the database, through an CodeIgniter model. I would like to automatically fetch and use the credit card's name in <title> so that i don't need to change it manually, but I'm a little stuck on how to proceed. This is my code as of now:

Rails validating search params

痴心易碎 提交于 2019-12-09 13:39:55
问题 I have an API which is fairly restful but am struggling to work out how to implement a search cleanly. I want to be able to search for all the records between two date-times, the date-times are allowed to be a maximum of 6 hours apart. At the moment in my controller method I have the following: required_params = [:start_time, :end_time] if check_required_params(required_params, params) and check_max_time_bound(params, 6.hours) ... rest of controller code here ... end check_required_params is

Transforming ActiveRecord validation errors into API consumable errors

我的未来我决定 提交于 2019-12-09 13:37:10
问题 I'm writing a pretty standard CRUD RESTful API in Rails 4. I'm coming up short on error handling though. Imagine I have the following model: class Book < ActiveRecord::Base validates :title, presence: true end If I try to create a book object without a title I'll get the following error: { "title": [ "can't be blank" ] } ActiveRecord validations are designed to be used with Forms. Ideally I'd like to match up each human readable validation error with a constant that can be used by an API

ActiveRecord error messages: translation for fields

允我心安 提交于 2019-12-09 13:31:24
问题 I've used the instructions specifies in http://guides.rubyonrails.org/i18n.html to translate fields of my model, but the labels doesn't come translated. What I'm doing wrong. I have a User model with the field name and I'd like to have it translated to Brazilian Portugues (pt_br), so I got my pt_br.yml: pt_br: errors: "Erro!" activerecord: models: user: "Usuário" attributes: name: "Nome" address: "Endereço" errors: template: body: "Por favor, corrija os campos assinalados" header: "Dados

problem: activerecord (rails3), chaining scopes with includes

白昼怎懂夜的黑 提交于 2019-12-09 13:17:23
问题 In Rails3 there seems to be a problem when chaining two scopes (ActiveRelations) that each have a different include: Consider these two scopes, both of which work fine on their own: First scope: scope :global_only, lambda { |user| includes(:country) .where("countries.area_id <> ?", user.area) } Work.global_only(user) => (cut list of fields from SQL for legibility) SELECT * FROM "works" LEFT OUTER JOIN "countries" ON "countries"."id" = "works"."country_id" WHERE (countries.area_id <> 3) Now

Adding and removing from a has_many :through relation

只谈情不闲聊 提交于 2019-12-09 13:11:29
问题 From the Rails associations guide, they demonstrate a many-to-many relationship using has_many :through like so: class Physician < ActiveRecord::Base has_many :appointments has_many :patients, :through => :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient < ActiveRecord::Base has_many :appointments has_many :physicians, :through => :appointments end How would I create and remove appointments? If I've got a @physician , do I

Ruby Gem Development - How to use ActiveRecord?

穿精又带淫゛_ 提交于 2019-12-09 12:35:45
问题 I'm currently trying to develop my first ruby gem and I'm already stuck. I used the "bundle gem" command to create the basic structure and read some tutorials but what I can't find is how to integrate ActiveRecord. Where do I create my migrations? Do I create the "db/migrations" folder within the lib folder or at the root? And do I have to do anything in the Rakefile (I found some questions where the answer was something like "you have to create your own [my_gem]:db:migrate" or something like