I'm building what I thought was a fairly simple recipe app while learning RoR.
I've got a table for users, and a table for recipes, and a recipe_users table where a user is saving a list of recipes.
The error I'm getting from rails is "uninitialized constant User::RecipeUser"
My Models are as follows
class User < ActiveRecord::Base
acts_as_authentic
has_many :recipe_users
has_many :recipes, :through = > :recipe_users
end
class Recipes < ActiveRecord::Base
has_many :ingredients, :dependent => :destroy
has_many :recipe_users
has_many :users, :through => :recipe_users
end
class RecipeUsers < ActiveRecord::Base
belongs_to :user
belongs_to :recipe
end
now in my users controller, I am attempting to call
#user = User.find(current_user.id)
#userRecipes = #user.recipes.find()
looking at my mysql Show Tables, I get
recipe_users
recipes
schema_migrations
user_sessions
users
so as far as I can tell, I've got the naming conventions right.
Any suggestions as to why I'm getting this error?
It looks like this was an issue of naming conventions.
I deleted all references to recipe_users and recreated the table, model and controller as meals.
Not a great name, but it all came together without a hitch.
I've never liked the naming conventions that rails seems to expect I think in part due to the pluralization without actually informing the developer of what name rails may be expecting.
Related
I am creating association pretty much identical with the Rails Guides Patient-Appointment-Physician data model. A user has many prospects through prospect_subscription. However, when trying to access user.prospects in rails console, it throws the following error:
Rails couldn't find a valid model for Prospects association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass. (NameError)
uninitialized constant User::Prospects (NameError)
Which is strange because all three models are right there. Migration has been run and sample data has been populated and can be checked in pgAdmin. Why can't Rails find the model?
Association defined at the models are as follows:
models/prospect.rb
class Prospect < ApplicationRecord
has_many :prospect_subscriptions
has_many :users, through: :prospect_subscriptions
end
models/user.rb
class User < ApplicationRecord
has_many :prospect_subscriptions
has_many :prospects, through: :prospect_subscriptions
end
models/prospect_subscription.rb
class ProspectSubscription < ApplicationRecord
belongs_to :user
belongs_to :prospect
end
I figured that wiping the database records clean and re-seeding helps. The difference is this time I assigned as user.prospects << [prospect_name], to make sure that the joins are created in the backend.
I am currently trying to set up a model structure that seems quite simple, but I haven't quite got it down.
I have a model payment that can belong to either a customer or a supplier (which can both have many payments).
My question is simply whether I need to manually create an interface table to allow this, or if declaring the polymorphic associations will do this for me?
e.g. I have:
class Payment < ActiveRecord::Base
belongs_to :payment_originator, :polymorphic => true
end
class Customer < ActiveRecord::Base
has_many :payments, :as => :payment_originator
end
class Supplier < ActiveRecord::Base
has_many :payments, :as => :payment_originator
end
Is this enough, or do I also need to use a generator to manually create the payment_originator model?
Thanks!
As far as the models go, this is good enough. You just need to migrate a :payment_originator_type and :payment_originator_id to the payments table. The associations you defined above will automatically fill these in for you.
I am currently using has_and_belongs_to_many to implement a many-to-many relationship. I would however want to put in a attribute in the many_to_many table.
Basically I am creating a email system. I have users and conversations. A user can have many conversations and a conversations can also have many users. However, I am trying to make it so that I can have a read/unread attribute to show which messages are read. Since conversations can have many users, it is not practicable to put the attribute in the conversations table as then it would mean that the conversation is read by all. So I think it would work best in the middle table. I am wondering though how I can access that attribute in the middle table. If the attribute is read. What code do I put in to access that and how do I update the attribute. As mention above I am using has_and_belongs_to_many
If you want to have additional attributes to your has-and-belongs-to-many association, you have to build a model class for that relation. See the detailed description in the Rails Guides about it.
After having read it for myself, this is now deprecated with the current version of Rails, so you really should switch to has_many :through. Your models could be (copied and changed from the Rails Guides, I don't know if connection is a good name for the m2n relation):
class User < ActiveRecord::Base
has_many :connections
has_many :conversations, :through => :connections
end
class Connection < ActiveRecord::Base
belongs_to :user
belongs_to :conversation
end
class Conversation < ActiveRecord::Base
has_many :connections
has_many :users, :through => :connections
end
There you are able to add additional attributes to your connections table, and refer in the code to them.
I have three Models setup with the following associations
class User < ActiveRecord::Base
has_many :faculties
has_many :schools, :through => :faculties
end
class School < ActiveRecord::Base
has_many :faculties
has_many :users, :through => :faculties
end
class Faculty < ActiveRecord::Base
belongs_to :user
belongs_to :school
end
and in my controller i go to create a school and assign the user
class SchoolsController < ApplicationController
def create
#school = current_user.schools.build(params[:school])
...
end
end
When I login and submit the form the flash displays success, but the association doesn't build on the join table.
I tried it inside the apps console and it builds the association just fine.
I've been stuck on this for a couple days now and I just cannot figure out what I am missing. Thank in advance for any and all advice
The build method does not save the object. You need to explicitly call #school.save.
Two things: If the schools association is :through a has_many association, you will have to select which parent the School exists through.
So, for instance, if you were to nest School resources under users as in /users/:id/faculties/:id you could create a school via current_user.faculties.find(params[:faculty_id]).schools.build(params[:school]).save
Based on the example code, it looks like the fundamental problem is that the has_many xxx, :through syntax is being used without specifying the id of the faculties record. Remember two things: 1) ActiveRecord doesn't natively support composite primary keys, and 2) you must call #save on associated records created using #build. If you remember these, you should be fine.
Just starting to use Ruby on Rails to see what its like.
I have a user model with an id and a post model with an adderId. The adderId of the post model should be the user id of the user that created it.
How can I relate these with Ruby on Rails?
The Rails convention for foreign keys would give your Post model a user_id column rather than adderId. You can break the convention, but that requires slightly more configuration, as below:
class User < ActiveRecord::Base
has_many :posts, :foreign_key => :adderId
end
class Post < ActiveRecord::Base
belongs_to :adder, :class_name => "User", :foreign_key => :adderId
end
If you gave Post a user_id instead, you could do this:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
I'd recommend taking a look at the Rails Guide for Active Record Associations, which covers all of the above and plenty more.
It looks like this question seems to be doing what you're trying to do.
Sorry if I misunderstood your question, I too am new to Ruby on Rails.