rails-models

Duplicate error message when validating a model

How is it possible that the model validation errors messages are getting duplicated in ruby on rails application? ...

Only one instance of a Model in Rails

I'm working on my first Rails app. It needs to store some information about a single advertisement that will appear on every page. The admin just needs to be able to set the URL, Title, and an image. Obviously, I only need one instance of this ad object. I created a model that inherits from ActiveRecod::Base, but that seems like the wro...

has_one and has_many in same model. How does rails track them?

I a little confused about how this work even if it works properly. I have a model that has two association to the same other model. Company has an owner and company has many employees of the class users. here is my company model: class Company < ActiveRecord::Base validates_presence_of :name has_many :employee, :class_name => '...

Importing redefined SQL data from old app to new one.

Hi, 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 was dramatically changed and some foreign key values should be updated. I am wonder...

Ruby On Rails - How to get the Object Method Caller

Let me explain my problem: I have 2 models: class User < AR::Base has_many :contacts end class Contact < AR::Base belongs_to :user belongs_to :user_contact_id, :class_name => "User", :foreign_key => "user_contact_id" # The "contact" is an ID from the table user. def self.is_contact?(user_contact_id) # CHECK IF THE RECORDS EXIST ...

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 table I need to include additional attributes beyond the keys such as the sales_relation betw...

In Rails, how should I implement a Status field for a Tasks app - integer or enum?

For a Rails 3.0 Todo app, I have a Tasks model with a Status field. What's the best way to store the Status field data (field type) and still display a human-readable version in a view (HTML table)? Status can be: 0 = Normal 1 = Active 2 = Completed Right now I have this: Rails Schema Here: create_table "tasks", :force => true...

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 so it will show up in my edit erb file. That way if there were no Addresses associated ...

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 User.create!(:email=>"[email protected]") and have it create a user with a randomly generated API key, and secret. I...

rails use counts in diferent views

Hello i guess this is going to be pretty noob question.. But.. I have an scaffold called list, which has_many :wishes. And with that information in my model, I can in my list view use this code <%=h @list.wishes.count %> well now I have made an controller called statusboard.. And in that' I have 3 functions.. Or how to say it.. b...

Rails - Debugging Nested Routes

Hi, I have 2 models, Assessments and Questions. Assessments have many questions. In routes, I have: map.resources :assessments, :has_many => :questions map.root :assessments I checked rake routes, it's as expected On the form to create a new question, I get the following error: undefined method `questions_path' for #<ActionView::B...

Belongs_to based on value of a field

Hello, I have a table with entries, and each entries can have different account-types. I'm trying to define and return the account based on the value of cindof Each account type has one table, account_site and account_page. So a regular belongs_to won't do. So is there any way to return something like: belongs_to :account, :class_nam...

How can I associate 2 models to another on my create action

I have 3 models with the following associations. A model Question belongs to a particular user and has many answers, an Answer model belongs to both a user and a question. A user model therefore has many questions and answers. My answer model has the fields User_id and question_id which identifies the owner of the question the answer is...

How do I model a table tennis match in rails.

I am attempting to model a table tennis match in rails. Here is what I have: Game Model: team_1_score team_2_score team_1_id team_2_id Team Model: game_id player_id Player Model: Name So each game will consist of 2 teams (of either 1 or 2 players each). Then I was planning on linking game to player with has_many, :through. I don'...