In my user model, I have a method display_name to output the user's full name. user.rb (id, first_name, last_name, email, ...) def display_name [first_name, last_name].compact.join(' ') end I'm trying to get my controller to return display_name like
Trip and Alert model are associate like: Trip has_many alerts I have an object like @alerts = Alert.all Now I want to find Array of unique Trip from @alerts How can i do this ?You could try something like: Trip.where(id: @alerts.map(&:trip_id).uniq)
I need to write a query that only counts individuals that have not attended an event previously. Here are the models class Individual < ActiveRecord::Base has_many :attendances, dependent: :destroy has_many :events, through: :attendances end class At
I have a Contact model that has an email attribute. Contact belongs_to :company and Company has_many :contacts. I would like to add a uniqueness validation on Contact that ensures that the email address is unique within the subset of Contacts that be
I am having real trouble accessing the value of an additional parameter called permission on a has_many through. It is probably something simple. My 3 Models are class User < ActiveRecord::Base has_many :players_users has_many :players, through: :pla
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
I've set up two models where an Album has a Genre and that Genre has many albums that belong to it. class Album < ActiveRecord::Base attr_accessor :genre_id belongs_to :genre end class Genre < ActiveRecord::Base has_many :albums end Here's my migrat
I would like to setup a before_create for all of my modules what i have been trying is: module ActiveRecord module UserMonitor require 'securerandom' before_create :attach_uuid def attach_uuid self.uuid = SecureRandom.uuid.gsub("-","")
We've built a dynamic questionnaire with a Angular front-end and RoR backend. Since there are a lot of dynamic parts to handle, it was impossible to utilise ActionView or jbuilder cache helpers. With each Questionnaire request, there are quite a lot
Suppose I have a one-to-many relationship between Album and Song classes. I would like to do some complex filtering on the albums and retrieve the corresponding songs. Let's assume a fairly simple way to filter albums filtered_albums = Album.where('r
I had a method in a model: class Article < ActiveRecord::Base def do_something end end I also had a unit test for this method: # spec/models/article_spec.rb describe "#do_something" do @article = FactoryGirl.create(:article) it "should w
//here is my function block public function create_accnt() { $post = $this->input->post(); //gets all possible posts in array form echo "Post array: ";print_r($post); // just to make sure that my array has values $data = array( 'userid' =&
I have no experience in SQl databases. I just started using ActiveRecord and I find myself compy pasting the ActiveRecord query statements beucase I don't know difference between when to use .find() .where(), joins etc. There's so many ways to do one
I'm trying to search a model for any dates equal to a specific date while omitting the timestamp. In Rails I could simply execute this as DateTime.to_date == somedate, however I don't think it's quite as easy to formulate in SQL where I wouldn't be a
Hello I have a peculiar problem. I use an after_commit callback to send notifications, but it seems the callback is not triggered at all. Simplified the situation looks like this: class Message < ActiveRecord::Base after_commit :do_something def do_s
This is a build on a question I asked before (without any luck). I have a model where I'm using a string as a primary key: class Employee < ActiveRecord::Base self.primary_key = "employment_id" end This table also contains the Rails default '
this is a real hair-puller. I've got a junction table that I need to query for 2 different entries, then update the junction table if the values are not there already. Essentially I have this: master_actors = [#<Actor2role actorId: 13176, dogTag: 459
I am writing code that will migrate some data from one database to another, over-writing some data in the destination. It uses ActiveRecord, since it's associated with a Rails app using AR already. Since some data will be over-written, I'd like to pr
I have a Task model and User model. A task can be assigned a user to process it, while a user can be assigned many tasks. The field names are a little off so I am trying to use the class_name options etc, but for whatever reason I'm getting some odd
This question already has an answer here: Rails select random record 7 answers I'm currently using: @users = User.order("RANDOM()").limit(6) to generate a list of 6 random users - however, this method takes a 1200ms toll on page load times. Is t
I am trying to include a virtual attribute/method within a respond_to JSON hash. The Model (employee.rb) attr_reader :my_method def my_method return "foobar" end The Controller (employees_controller.rb) respond_to :json def index @employees = Em
I've written a Ruby script that parses a tab-delimited text file and writes the output to a sqlite3 database with ActiveRecord. Next, I need to write another Ruby script to display the database's contents as static HTML. The text file that I'm workin
I don't know if this is even possible. Anyway, here is my problem: I want to create a Class having a database table schema, for example suppose that I have a table like id - unsigned int username - varchar(128) password - varchar(128) and let's assum
I have a datetime field called time in my MotionRecord model. I try to set it using this command: MotionRecord.create({:time=> "2010-10-15 15:10:24", :chart_id=>1}) Oddly enough this results in the following input: <MotionRecord id: 1,
What's the standard/best option out there for this kind of version control? The main thing I'm looking for is to track what user edited a record. I've seen these so far and am wondering what your take is: PaperTrail ActsAsAudited VestalVersionsThe pl
I have several similar Models (ContactEmail, ContactLetter, ContactPostalcard). Each instance (record) in, say, ContactEmail, means a specific Email template was sent to a specific Contact. So, each one (e.g. ContactEmail) belongs_to :contact So, in
Is it possible to use delegate in your Active Record model and use conditions like :if on it? class User < ApplicationRecord delegate :company, :to => :master, :if => :has_master? belongs_to :master, :class_name => "User" def has_mas
I've looked everywhere for an elegant solution. The essential problem seems to be that ActiveRecord attributes that map to database columns are handled completely differently in ActiveRecord::Base than attr_accessor methods. I would like to do someth
I'm just learning how to program in Ruby on Rails, and I'm really impressed by how much work the Ruby on Rails framework does for you. I have the following two classes: Object -------- object_id description ObjectGroup -------- group_id description T
I'm trying to remove the commas from a field in a model. I want the user to type a number, i.e. 10,000 and that number should be stored in the database as 10000. I was hoping that I could do some model-side normalization to remove the comma. I don't