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
1
vote
2 answers
How do I create two relationships to the same model with different names?
I have two Rails 4 models: Journey and Place. I would like a Journey two have two fields, origin and destination, that are both Places. My Journey class looks like this:
class Journey < ActiveRecord::Base
has_one :origin, class_name: :place
…

Allan Lewis
- 308
- 3
- 13
1
vote
2 answers
Ownership of models with Devise?
I'm using Devise for the first time with my Rails 4 project and I am trying to figure out how to lock down ownership of models to certain users.
For example, if a user creates a model only that user should be able to do the following to it,
list…

James McMahon
- 48,506
- 64
- 207
- 283
1
vote
1 answer
Rails - model association for recursive user model relationship
In my user model, I have
belongs_to :admin_creator, foreign_key: :created_by_user_id, class_name: "User"
and in my create controller action I have...
def create
@user = User.new(user_create_params)
@user.created_by_user_id =…

user1322092
- 4,020
- 7
- 35
- 52
1
vote
3 answers
Rails - MVC - Should I move an external API call to the model?
I'm very new to rails and MVC development in general and have been working on an app which makes an external API call to bitly.
I've place the following in the view to generate a shorturl which I then use for fb and twit.
<% Bitly.use_api_version_3
…

Elliott de Launay
- 1,027
- 13
- 31
1
vote
1 answer
Create action inside another controller
I am attempting to create a new User inside a controller for my Request class, but have having some difficulty. Below is my 'create' action inside my Requests controller. I realize I can't just call User.new, but am unsure how to structure the…

Bryon Finke
- 11
- 1
1
vote
1 answer
ruby on rails multiple tables in one model
I am using Ruby on Rails and I have to create an importer from one database to another. There are over 100 tables in each database and I don't want to create a model for each table. Is there any possibility to create queries, specifying a table…

staskjs
- 97
- 6
1
vote
2 answers
Using a method within model, calling it from view
I have an Update model which belongs to users.
To show all of one user's friends' Updates, I am doing something like:
Update.where("user_id" => [array_of_friend_ids])
I know the "right" way of doing things is to create a method to create the…

jenno
- 71
- 1
- 2
- 7
1
vote
1 answer
Rails: uniqueness validation using array for scope
I'm doing a tags model where one of the attributes is the title of the tag. The goal is that there will be articles that can be tagged by a predefined set of topics:
TOPICS = ['Politics', 'Art', 'Sports', 'Tech', 'Business', 'Science']
I would like…

Adler Santos
- 365
- 5
- 19
1
vote
2 answers
Rails attachment on polymorphic model
I've tried many, many things so I'm not even sure which code/errors to give. I'll give my most generic code.
Models: Job, Attachment
Attachment - polymorphic association through attachable
Job Model
has_many :attachments, as:…
user2156026
1
vote
2 answers
Rails user profile devise :has_many or :has_many_through relationships
I'm wracking my brain about how to start setting up the following set of model relationships in my project. I'm not looking for the complete solution (models, tables, migrations, etc), just a little help to get me going in the right direction. I…

Kyle Carlson
- 7,967
- 5
- 35
- 43
1
vote
1 answer
Getting current_user's id in model
Forgive me if this is a newb question but I was wondering how they got the current user's id in the User model here:
Listing 10.39
I've tried reading it again and again and i still can't figure it out :(
class User < ActiveRecord::Base
.
.
.
…

Skyalchemist
- 461
- 1
- 7
- 18
1
vote
1 answer
How to solve: validates.rb:96:in `rescue in block in validates': Unknown validator (...)
I'm start learning Rails, and I can't go forward because I'm getting a error when I try to run:
bundle exec rspec spec/
I think the error is on my "models/users.rb" file:
class User < ActiveRecord::Base
attr_accessible :email, :name
…

Paladini
- 4,522
- 15
- 53
- 96
1
vote
1 answer
Query that joins child model results item erroneously shown multiple times
I have the following models, each a related child of the previous one (I excluded other model methods and declarations for brevity):
class Course < ActiveRecord::Base
has_many :questions
scope :most_answered,…

daspianist
- 5,336
- 8
- 50
- 94
1
vote
1 answer
Multi-layer sorting in Rails model
Im having trouble figuring out how to write a multi-layer sort using a scope method in my model, which can sort through the model's attributes, as well as its related child models' attributes?
To put more concretely, I have the following models,…

daspianist
- 5,336
- 8
- 50
- 94
1
vote
2 answers
Using scope to sort entries in related child models
I have three related models such as the following, each being the child of the one above:
class Course < ActiveRecord::Base
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :course
has_many: :answers
default_scope…

daspianist
- 5,336
- 8
- 50
- 94