a component of the Rails framework that holds the state of an object and is responsible for enforcing business rules and persisting the object.
Questions tagged [rails-models]
367 questions
0
votes
1 answer
Does rails 3 give options to add filter inside model like validations?
Example Case- How add filter to strip all html tags(this code not works, just psedocode for want i need to get):
class Person < ActiveRecord::Base
validates :name, :presence => true
#psedocode:
**filters:name,:strip_tags=>true**
end

Ben
- 25,389
- 34
- 109
- 165
0
votes
0 answers
Rails 3.2 Security
In my rails app I have a User Model where I have an attribute: points
class User < ActiveRecord::Base
attr_accessible :points
has_many :answers
end
Is there a way to make the attribute updated only from specific controller actions?
class…

RickyonRails
- 13
- 1
- 6
0
votes
1 answer
Rails: Trying to separate a model into an engine gem
I'm new to here an to Rails so excuse any dumbness on my part.
I'm trying to do something really simple and can't seen to find a guide that's up to date and properly demonstrates how to do this.
I've written the app described in the official ruby on…

Shrewd
- 731
- 5
- 14
0
votes
2 answers
Model inheritance – use 'where' depending on attribute
I have a user model and a dealer and customer model, which inherit the user class and have no own database table:
class User < ActiveRecord::Base
end
class Dealer < User
before_save :set_default_values
def set_default_values
self.role_id =…

Arne Cordes
- 581
- 1
- 6
- 22
0
votes
1 answer
How to correctly implement a form in rails?
I'm developing my first web application in rails and I'm pondering whether this is a good or a bad way to implement a functionality.
This web application has a User and Guild classes.
The objective is that the user answers a set of questions through…

rdlf
- 25
- 5
0
votes
3 answers
Rails: combine two queries into one
I am new to rails. Here is the following code with Foo as model object:
a = Foo
a = Foo.where(age: 18)
if params[:sort] == "desc"
a = a.order("name desc")
end
Here two queries are performed, I want to combine them to one or you can say i want to…

Paritosh Singh
- 6,288
- 5
- 37
- 56
0
votes
1 answer
Rails Create has_record? for a nested model
I've two models
class Article < ActiveRecord::Base
has_one :review
end
class Review < ActiveRecord::Base
belongs_to :article
end
Now I would like to have this method in Article
class Article < ActiveRecord::Base
has_one…

user200302
- 7
- 4
0
votes
2 answers
Rails Undefined method for ni:NilClass Error?
I have a rails app where I have has_many and belongs_to association. But I am having problems while trying to retrieve the elements. My Models are Events, Comments and Votes.
Events has multiple Comments and Votes. Of Course Comments and Voted…

user1364813
- 13
- 4
0
votes
1 answer
Devise: How to customize the registrations controller destroy method
I have my user and profiles in separate models. When a user is deleted the linked profile remains, which is the desired result. what I would like to do is flag the profile record as deleted.
I have added a deleted column(boolean) to my profile…

Aaron Dufall
- 1,177
- 10
- 34
0
votes
4 answers
How to use foreign key in model and form builder?
I have two models: User and Location as below:
class User < ActiveRecord::Base
attr_accessible :location, :password, :user_name, :password_confirmation
validates :location, :user_name, :presence => true
validates :password, :presence => true,…

zhu0qun
- 3
- 1
- 4
0
votes
1 answer
Creating Multi-Dimensional Hashes in Ruby From ActiveRecord Results
I am new to rails and am having troubles figuring out some things with ActiveRecord.
Right now, I have three models:
class Project < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :tags
has_many :tasks
end
class Task <…

unlikely_monkey
- 183
- 4
- 15
0
votes
1 answer
Rails 3: Pass options to model method
This is my Model (Subscription) Method:
def activation_codes(options = {})
if options[:first]
self.group.group_codes.first
else
self.group.group_codes
end
end
I am trying to call to this method in this fashion:
sub =…

dennismonsewicz
- 25,132
- 33
- 116
- 189
0
votes
1 answer
Rails + model.rb issues -- rails console error message -- "Object doesn't support #inspect"
EDIT3:
To wit, DO NOT put migration code in your model.rb files!!!
EDIT2: THE QUESTION (?) :
does ANY migration code belong in a model.rb file?
EDIT: Just mentioning extra (system/config/etc) information that I need to share in order to get a good…

boulder_ruby
- 38,457
- 9
- 79
- 100
0
votes
2 answers
set attribute from value of method
I am sure I am just doing this wrong, can someone point me in the right direction?
I need to set the value of effective_date to a modified format date
effective_date is an attribute of Request
class Request < ActiveRecord::Base
def…

Toby Joiner
- 4,306
- 7
- 32
- 47
0
votes
1 answer
Custom Model Method, setting scope for automatic sending of mail
There are several stages to this, and as I am relatively new to rails I am unsure if I am approaching this in the best way.
Users follow Firms, Firms applications open and close on certain days. If a user follows a firm I would like them to…

RMcNairn
- 491
- 1
- 5
- 20