All Questions

Tagged with
Filter by
Sorted by
Tagged with
240 votes
29 answers
134k views

Is there a way to get a collection of all the Models in your Rails app?

Is there a way that you can get a collection of all of the Models in your Rails app? Basically, can I do the likes of: - Models.each do |model| puts model.class.name end
mr_urf's user avatar
  • 3,343
210 votes
5 answers
119k views

Rails find_or_create_by more than one attribute?

There is a handy dynamic attribute in active-record called find_or_create_by: Model.find_or_create_by_<attribute>(:<attribute> => "") But what if I need to find_or_create by more than ...
tybro0103's user avatar
  • 49.1k
126 votes
8 answers
117k views

Using helpers in model: how do I include helper dependencies?

I'm writing a model that handles user input from a text area. Following the advice from http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input, I'm cleaning up the input in the model ...
O. Frabjous-Dey's user avatar
94 votes
10 answers
68k views

Using ActiveRecord, is there a way to get the old values of a record during after_update

Setup using a simple example: I've got 1 table (Totals) that holds the sum of the amount column of each record in a second table (Things). When a thing.amount gets updated, I'd like to simply add the ...
Abel's user avatar
  • 2,135
40 votes
4 answers
30k views

How to implement Active Record inheritance in Ruby on Rails?

How to implement inheritance with active records? For example, I want a class Animal, class Dog, and class Cat. How would the model and the database table mapping be?
andrisetiawan's user avatar
40 votes
2 answers
29k views

Could not find the association problem in Rails

I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own. Given the three model classes with their associations: # application_form....
Ash's user avatar
  • 25.1k
40 votes
6 answers
12k views

rails model has_many of itself

I have a event model. Events can have parent events, set from a column in the model (parent_event_id). I need to be able to do has_many :event on the model, so I can just do, for example, event....
Alexander Forbes-Reed's user avatar
39 votes
6 answers
9k views

Rails Models: how would you create a pre-defined set of attributes?

I'm trying to figure out the best way to design a rails model. For purposes of the example, let's say I'm building a database of characters, which may have several different fixed attributes. For ...
Andrew's user avatar
  • 42.8k
30 votes
3 answers
5k views

problem with passing booleans to update_attributes

I've got the following Model: class GuestCatering < ActiveRecord::Base # Validation validates :name, :presence => true validates :order_number, :presence => true validates :...
LeonS's user avatar
  • 2,704
27 votes
5 answers
1k views

How to model interpretations of rap music

I just started working on a website that will help people understand what rappers are talking about. Users will see the lyrics to a rap song and they'll be able to click certain lyrics to see an ...
Tom Lehman's user avatar
  • 87.7k
25 votes
5 answers
12k views

validate at least one in has_and_belongs_to_many

I have a model with: has_and_belongs_to_many :users How do I validate that the model has at least one user in the model? I tried: validates_presence_of :users But that doesn't seem to give me ...
cmaughan's user avatar
  • 2,604
23 votes
8 answers
45k views

How would I use ON DUPLICATE KEY UPDATE in my CodeIgniter model?

I have a CodeIgniter/PHP Model and I want to insert some data into the database. However, I have this set in my 'raw' SQL query: ON DUPLICATE KEY UPDATE duplicate=duplicate+1 I am using CodeIgniter ...
Jack's user avatar
  • 9,693
23 votes
4 answers
7k views

Rails - Where (directories) to put Models that are not Active Record

We are building out apps that have Models that are not database components. We are curious to learn what others are doing in the rails community to address this subject. We are struggling with where ...
Michael Durrant's user avatar
22 votes
2 answers
14k views

Ruby on Rails: How to validate a model without Active Record?

I'm currently trying to validate fields without having an ActiveRecord::Base inheritance. My model stores the data on a cache server so I do not need ActiveRecord. Anyway, I would like to validate the ...
Nicolas Guillaume's user avatar
20 votes
4 answers
16k views

How to override a column in Rails model?

I have a model for one of my database tables. I want to override the column name for that particular table. How would I achieve it. For example, let my table be called DUMMY and it has one column ...
bragboy's user avatar
  • 35.3k
20 votes
3 answers
11k views

Rails: is it possible to add extra attribute to a has_and_belongs_to_many association?

What I mean is if I have two models, connected by a has_and_belongs_to_many association, can I store other data in the join table for each association? That is, the extra data would not be part of a ...
David Robertson's user avatar
19 votes
3 answers
3k views

How do I specify shared attributes translations between models in I18n locale file?

I tried the following: es: activerecord: attributes: name: Nombre And it did not work. But the following do work: es: activerecord: attributes: person: name: Nombre ...
Zequez's user avatar
  • 3,429
17 votes
1 answer
17k views

Eager loading in deep level nested association

I have models having nested associations. I want to load all nested records from just one query. hotel has_many rooms rooms has_many room_variants room_variant has_many seasonal_rates I search for ...
user1787700's user avatar
17 votes
7 answers
17k views

How to create ActiveRecord tableless Model in Rails 3

I am trying to create a Active Record tableless Model. My user.rb looks like this class User < ActiveRecord::Base class_inheritable_accessor :columns def self.columns @columns ||= []; ...
Achaius's user avatar
  • 6,054
17 votes
1 answer
21k views

Rails: How do self-referential has_many models work?

So, I'm working on an app where I want to users to be able to group objects in "folders". Basically: User has_many :foos Foos don't have to be in a folder, but they can be. In that case: Folder ...
Andrew's user avatar
  • 42.8k
17 votes
3 answers
6k views

Namespaced models in Rails: What's the state of the union?

Since the beginning, Rails has had issues with namespaced models. As time went on, pretty much everybody gave up on using it. Myself included. With Rails 2.3 out, I'd like an update on the situation. ...
kch's user avatar
  • 78.5k
16 votes
1 answer
9k views

rails has_one of a has_many association

I have a product, and a product can have many images. This is through an associations table. However, I would like a product to have one main image. I know this is very easy to do with a method in ...
Steph Rose's user avatar
  • 2,136
14 votes
6 answers
8k views

Rails gem rails3-jquery-autocomplete: How do I query multiple fields

I'm using the rails3-jquery-autocomplete gem found here: http://github.com/crowdint/rails3-jquery-autocomplete The instructions are clear for how to query a single attribute of a model and I am able ...
Clay's user avatar
  • 2,969
12 votes
2 answers
10k views

rails g scaffold for existing model and DB table

I would like to create a structure with rails g scaffold Article, but I have already created the table Articles and model Articles. Is there any way to do that?
user984621's user avatar
  • 47.4k
12 votes
1 answer
5k views

Ruby on Rails ActiveRecord: pluralization

I'm new to Rails, so forgive my ignorance of ActiveRecord. One of my models is named "campus". I ran the migration and it pluralized everything except "campus". I thought that was lame so I added ...
Donald Hughes's user avatar
12 votes
2 answers
1k views

has_one, :through => model VS simple method?

I have some issues using has_one, through => model. The best is to show you my case. class Category has_many :articles end class Article has_many :comments belongs_to :category end class ...
Hartator's user avatar
  • 5,127
11 votes
2 answers
30k views

How to update only specific fields of an active record in Yii?

I have a model (ActiveRecord) that has 5 properties (DB columns). I fetch a specific record and populate a form that has 3 fields (two other fields shouldn't be updated). Then I change a specific ...
lvil's user avatar
  • 4,326
11 votes
3 answers
5k views

In rubyonrails, how to get the associated model class from and ActiveRecord::Relation object?

Suppose I have an model: class Post end posts = Post.where(***) puts posts.class # => ActiveRecord::Relation Then how can I get the model class name through the variable 'posts', maybe ...
Croplio's user avatar
  • 3,503
10 votes
3 answers
8k views

Duplicating a record in Rails 3

I have a prescription model in my Rails 3 application. I am trying to work out the best method of allowing records to be duplicated, but allowing the user to "review" the duplicate before it's saved. ...
dannymcc's user avatar
  • 3,784
9 votes
1 answer
8k views

find_by_sql with array format in Rails 3

good day guys! I'm using find_by_sql() in rails 3 to fetch records as follows. @list=Email.find_by_sql(["SELECT * FROM Emails WHERE sent_id=?",params[:id]]) How to modify the same statement if ...
Unknown Coder's user avatar
9 votes
1 answer
4k views

Yii2: Prevent empty string in attribute of ActiverRecord

What's the best way to prevent that an empty string gets inserted into a field of an MySQL's InnoDB table? Allowed values are strings with more than zero characters or NULL. I'm asking that because ...
robsch's user avatar
  • 9,518
9 votes
6 answers
2k views

How do I force a Rails query to return potentially two models per result?

I'm using Rails 5. I have this in my controller model for loading a certain model subject to criteria ... @results = MyObjectTime.joins(:my_object, "LEFT JOIN ...
Dave's user avatar
  • 16.4k
8 votes
2 answers
15k views

Update db/migrate after manually updating models?

For example i have this model: class Product < ActiveRecord::Base attr_accessible :name, :order end Then when i did rake db:migrate it created this db/migrate/20120825132038_create_products.rb: ...
Danpe's user avatar
  • 18.9k
8 votes
2 answers
3k views

What is the "rails way" to access a parent object's attributes?

Let's say I have a model Doctor, and a model Patient. A Patient belongs_to a Doctor. A Doctor has an attribute office. I would want to, given a Patient p, be able to say p.office and access the ...
Tim's user avatar
  • 6,159
8 votes
3 answers
3k views

Are there any ETL tools that integrate with Rails models?

I'm researching ETL tools to import flat files into a database and subsequently export xml files. Many of the tools support generating code to use in your application; however, I haven't found any ...
Kyle West's user avatar
  • 9,008
7 votes
4 answers
15k views

Rails Model: Name -- First, Last

I'm fairly new to rails, working on a Rails 3 app with a Profile model for users. In the profile Model I'd like to have a "name" entry, and I'd like to be able to access logical variations of it ...
Andrew's user avatar
  • 42.8k
6 votes
3 answers
9k views

global methods in ruby on rails models

I have methods in all of my models that look like this: def formatted_start_date start_date ? start_date.to_s(:date) : nil end I would like to have something that automatically writes a method ...
Chris Drappier's user avatar
6 votes
2 answers
15k views

Yii - multiple records in one form submission

Does anyone know how to add multiple records using just one form in Yii? All the records belong to the same model and are of the same format. Many thanks, Nick
goose's user avatar
  • 2,542
6 votes
1 answer
2k views

Triggering dependent: :destroy with overridden destroy-method

In our application we've overridden the ActiveRecord destroy method so that our records do not get deleted (so the user can undelete). Like so: def destroy self.is_deleted = true self.save ...
Jocke Selin's user avatar
6 votes
2 answers
2k views

Rails validating virtual attributes

I this model: class Bunny < ActiveRecord::Base attr_accessor :number validates_presence_of :number validates_numericality_of :number end Whenever I submit a form to create this model ...
tybro0103's user avatar
  • 49.1k
5 votes
2 answers
1k views

counter_cache has_many_through sql optimisation, reduce number of sql queries

How I can optimise my SQL queries, to ignore situations like this: Meeting.find(5).users.size => SELECT COUNT(*) FROM ... WHERE ... User.find(123).meetings.size => SELECT COUNT(*) FROm ... ...
astropanic's user avatar
  • 10.9k
5 votes
2 answers
6k views

What is the best practice for testing Models in Rails?

How much or how little should one test models in rails? Since the framework is basically doing so much for you, I'm wondering if it's worth testing the generated activerecord methods to make sure they ...
egervari's user avatar
  • 22.4k
5 votes
2 answers
5k views

Ruby on Rails: Fully functional tableless model

After searching for a tableless model example I came across this code which seems to be the general consensus on how to create one. class Item < ActiveRecord::Base class_inheritable_accessor :...
Paul Ridgway's user avatar
  • 1,065
5 votes
2 answers
1k views

Creating a status attribute for a model

I want to create a status attribute for my Taskmodel that would indicate where it is in a three part progress in this order: open => in-progress => complete. It would work in a way similar to how an ...
Carl Edwards's user avatar
  • 14.1k
5 votes
2 answers
10k views

Yii2: How to check if activeRecord Model is Empty

I am trying to fetch data from database like this: $model = ProductFiles::findAll(['product_id' => $product_id]); When I check count($model), it returns as 0 if the model is empty and when the ...
Saani's user avatar
  • 862
5 votes
4 answers
6k views

rails model attributes without corresponding column in db

I have some rails models which don't need any persistence, however I'd like rails to think the model actually has attributes x, y, z so when calling methods like to_json in the controller I get them ...
nc.'s user avatar
  • 105
5 votes
2 answers
7k views

Rails pass additional parameter to a model

in a controller I have a create action def create params[:note] @note = current_user.notes.new(params[:note]) if @note.save respond_with @note, status: :created else ...
Matteo Pagliazzi's user avatar
5 votes
4 answers
9k views

Rails after_create callback can't access model's attributes

I can't access my model's attributes in the after_create callback... seems like I should be able to right? controller: @dog = Dog.new(:color => 'brown', :gender => 'male') @dog.user_id = ...
tybro0103's user avatar
  • 49.1k
4 votes
3 answers
2k views

Problem creating ActiveRecord model: data missing from save

I'm having trouble creating a new model row in the database using ActiveRecord in a Sinatra app I'm developing. The object in question is being created without any errors (using save!, no exceptions ...
localshred's user avatar
  • 2,224
4 votes
1 answer
2k views

Problem saving model in Rails

I'm building a simple blog with comments. There is a Post model and a Comment model. Every interaction between the two works fine except for creating new comments. I'm having an issue in Rails when ...
localshred's user avatar
  • 2,224

1
2 3 4 5
11