Questions tagged [rails-models]

a component of the Rails framework that holds the state of an object and is responsible for enforcing business rules and persisting the object.

367 questions
1
vote
1 answer

Model design for addons in an app

I have a Rails app in which the user can subscribe to addons, the list is dynamic and contains currently about 10 addons. The list specifies which is on/off. Each addon has a rather unique set of properties. My current solution is that the…
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
1
vote
2 answers

need to use `memoize` in custom module in rails3

I have a module in lib folder. Where I have created a InstanceMethods module. require 'memoist' extend Memoist module MyStudentMethods def self.included base base.send :include, InstanceMethods end module InstanceMethods def…
1
vote
2 answers

Rails table name contains digits

Is it possible to use a table in rails that contains digits in its name? I have a table named photo_2014_hierarchies Its model is saved as photo_2014_hierarchy.rb and inside I use class Photo2014Hierarchy < ActiveRecord::Base which works for…
1
vote
3 answers

More than one has_many :through?

So. I have users and movies. Users have watched some movies and not others. I want to express this relationship something like this: Note: Not sure if it matters; but movies don't have to be connected to a user; they can exist independently (i.e.…
1
vote
5 answers

How to setup default attributes in a ruby model

I have a model User and when I create one, I want to pragmatically setup some API keys and what not, specifically: @user.apikey = Digest::MD5.hexdigest(BCrypt::Password.create("jibberish").to_s) I want to be able to run…
Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74
1
vote
2 answers

Why is my model firing the validation at the wrong time?

In my edit action of my employees_controller I have this line of code: #Employee#edit 69: if @employee.user.person.addresses.length == 0 70: @employee.user.person.addresses << Address.new 71: end which should add a blank Address if there are none…
1
vote
1 answer

Rails 4: Saving multiple instances of same model in a single form

I have a survey app that allows people to create questions. When a user creates a new question they can also supply answer options. For Ex. Question: What color is the sky? Answer Options: Blue, Red, Purple. Right now, my single form allows for the…
thedeepfield
  • 6,138
  • 25
  • 72
  • 107
1
vote
0 answers

Rails: Best way to stub out a model with a string ID?

Say I want to stub out a Product model which will have a product_id field of type string. Do I add product_id in addition to the default id field that Rails creates?1 rails g model product product_id:string:uniq Or make product_id the primary…
Dennis
  • 56,821
  • 26
  • 143
  • 139
1
vote
2 answers

Rails logic best practices

I had some calculations on my controller like this @travel.food_expenses.map { |e| e.value * e.amount }.sum I know I shouldn't have the application logic on the controller, so I created a method on the Travel model def self.sum_food_expenses …
1
vote
0 answers

Regarding concerns in Rails 4 (multiple engines)

Update! I have sorted out the issue with the concerns, although I definitely appreciate the ClassMethods suggestion, as it seems to improve the code quality. The issue with the concern was (and it was a dumb mistake) I removed a concern and…
Mylan Connolly
  • 494
  • 1
  • 5
  • 15
1
vote
1 answer

Rails Model - attr_accessor raise unknown method exception?

The short version (dummy code): So I have one ActiveRecord (rails model) class: class User < ActiveRecord::Base attr_accessor :somevar end When I do @u=User.find(1) @u.somevar = 1 I get undefined method `somevar=' for…
1
vote
1 answer

Rails 4. Two model in view. Unknown number of object in second model

I have 2 models, for example model_1 and model_2. Model_1 has_many model_2. For model_1 I have "new" and "create" actions and one view for create objects of both models. In view I used "form_for" for object model_1 and "fields_for" for object…
NYV
  • 85
  • 1
  • 7
1
vote
2 answers

Rails Model Associations for Item-Item Relationship?

Looking for some guidance on the best way to implement this scenario: I have an items table (of products) and want to support the ability to cross-sell / up-sell / complement items. So there is an item-to-item(s) relationship here. In this join…
keruilin
  • 16,782
  • 34
  • 108
  • 175
1
vote
1 answer

Importing redefined SQL data from old app to new one

I've nearly finished rewriting (with some new features) my Symfony (left a bit bad taste in mounth) application into Rails. There is some amount of data which I need to import from my old application into the new Rails app. Problem is that schema…
BlackTea
  • 1,274
  • 15
  • 27
1
vote
2 answers

Injecting form options and input tags from the model

How do I add options and fields to my form generated by a builder with form_for @user in the model? (ie. without touching the HTML) The reason I want to do that is that I am adding a pluggable module to my model, and I want it to automatically (a)…
Jonathan Allard
  • 18,429
  • 11
  • 54
  • 75