i have a (hopefully) simple question that might have been answered before.. i just couldnt find it... well, here we go, should be easy enough.
I have this schema
create_table "items", :force => true do |t|
t.text "description"
t.string "priority"
t.date "date"
t.time "time"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "done"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
And I have this in my form for adding a new item:
<%= collection_select(:item, :user_id, User.all, :id, :name) %>
Now, it works, data is saved correctly (I already set up the proper correlations). What i want though, is to display in the items index, the name of the person the item is assigned to, instead of just an ID number.
In items/index I have:
<td><%= item.user_id. %></td>
but i rather want something like
item.user.name
only, it won't work - I guess I need some action in my controller.
Can you help me please? :)
EDIT here is some more details:
My models:
class User < ActiveRecord::Base
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :user
end
Add this to your Items class:
class Items < ActiveRecord::Base
belongs_to :user
end
Then item.user.name should work.
Related
So I am new to Rails and I have been trying to build a nested form. I have been having a lot of trouble and can't seem to get it to work.. I have watched multiple videos on youtube but I can't seem to find what I am doing different. For the purpose of me trying to build one, I have a Product which has many Buyers but a Buyer belongs to only one Product. (Assume you can only buy one Product...). When I submit my form I get an error which I can see in the server log: "Unpermitted parameter: buyer" I feel like I have tried everything.. I'd be so happy if someone could maybe tell me whats going on. Thanks so much
I have followed the Rails guide and added the following to my models:
class Product < ActiveRecord::Base
has_many :orders
has_many :buyers
accepts_nested_attributes_for :buyers
end
class Buyer < ActiveRecord::Base
belongs_to :product
end
Strong Params in the Product Controller:
def product_params
params.require(:product).permit(:name, :description, :image_url, :color, :adult, buyers_attributes: [:name, :age, :product_id])
end
And the Products controller:
def new
#product = Product.new
#product.buyers.build end
Then for the form:
Form
(Sorry, was having major issues inserting the code here)
Lastly this my schema for both tables:
create_table "buyers", force: :cascade do |t|
t.string "name"
t.integer "age"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "product_id" end
`
create_table "products", force: :cascade do |t|
t.string "name"
t.text "description"
t.string "image_url"
t.string "color"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "price"
t.binary "adult"
end
Your product acceptes nested attributes for buyers but you're only adding buyer (no plural) attributes to the form. You probably need to change the nested form to
<%= f.fields_for :buyers, [#product.buyers.build] do |x| %>
I have two models "Events" and "Categories". Event model has integer type "category" that belongs_to "Category". Category model has key:value that stores these categories such as 1: Medical 2: Health 3: Food, etc.
class Event < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :events, foreign_key: "category"
end
My schema is:
create_table "categories", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "events", force: true do |t|
t.string "name"
t.integer "category"
t.datetime "created_at"
t.datetime "updated_at"
end
I am using simple forms and maybe this is where I am going wrong but I am returning the right value but just as a string. For example, if I selected medical from the drop down it would be return the correct "1" but just as a string and not the required integer.
/views/events
<%= f.input :category, as: :select, collection: Category.all, include_blank: "Select a category..." %>
My drop down is displaying properly and returning the correct value but as a string and not an integer. Is there a best way to fix this?
You should have f.input :category_id, as that is what is being submitted, the ID of the category you select, not the category itself.
I'm stuck with this issue.
The RoR doc about this topic is really awful.
I don't understand how collection_select works.
I have two models : Skills and Projects.
A Project have many skills and a skill have many projects.
So here are my schemas :
create_table "projects", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects_skills", id: false, force: true do |t|
t.integer "project_id"
t.integer "skill_id"
end
create_table "skills", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "mastering"
end
The Project Model
class Project < ActiveRecord::Base
has_and_belongs_to_many :skills
end
The Skill Model
class Skill < ActiveRecord::Base
has_and_belongs_to_many :projects
end
And the collection_select
= form_for(instance_variable_get('#' + controller.controller_name.singularize)) do |f|
.field
= f.label :skills
= collection_select(:skills, :id, Skill.all, :id, :name, {:multiple=>true})
.actions
= f.submit
The select appears well populated, but nothing is persisted in my database.
Maybe someone can see a mistake in my code ?
Thanks
I have read about them but still not clear to me which one I suppose to use and how.
I have User model, Message model and Place model
Message model:
class Message < ActiveRecord::Base
belongs_to :user
end
Messages Table:
create_table "messages", force: true do |t|
t.string "title"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
User model:
class User < ActiveRecord::Base
has_many :messages
end
Users Table:
create_table "users", force: true do |t|
t.string "email"
t.string "password_digest"
t.datetime "created_at"
t.datetime "updated_at"
t.string "username"
end
Now, what I want to do is:
"USER" says "MESSAGES" from "PLACES"
eg. "AHMED" says "HELLO" from "EARTH"
For me both Models (Message and Place) have same data (data type) and same behaviours. So places table should be:
create_table "places", force: true do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Now may be I'm confused or making big deal than it should be.
What kind of relation should Message and Place have? should it be STI or Polymorphism?
How should I decide?
I'd appreciate the thinking process of how and why I decide specific association.
This example, despite Messages and Places having the same data, doesn't seems a STI/Polymorphism scenario and they should have two different tables.
This could work as a solution:
create_table "users" do |t|
t.string "username"
end
create_table "messages" do |t|
t.string "text"
t.integer "user_id"
t.integer "place_id"
end
create_table "places" do |t|
t.string "name"
end
class User < ActiveRecord::Base
has_many :messages
has_many :places, through: :messages
end
class Place < ActiveRecord::Base
end
class Message < ActiveRecord::Base
belongs_to :user
belongs_to :place
def to_s
"#{user.username} says #{title} from #{place.name}"
end
end
ahmed = User.new(username: "AHMED")
earth = Place.new(name: "EARTH")
message = Message.new(text: "HELLO", user: ahmed, place: earth)
puts message
# => "AHMED says HELLO from EARTH"
I have a couple of objects in a Rails app ("Ticket", and "Comment")
class Ticket < ActiveRecord::Base
has_many :attributes
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :ticket
belongs_to :user
end
with the following schema:
create_table "comments", :force => true do |t|
t.integer "ticket_id"
t.integer "user_id"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tickets", :force => true do |t|
t.integer "site_id"
t.integer "status"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
However, for some reason - whenever I do a #lead.comments I get a crash:
can't convert String into Integer
Any ideas? This is driving me nuts!
I think the line that's causing you pronlems is:
has_many :attributes
"attributes" is a special word in an Active Record. It refers to the values of the columns in the db.
If you try and override this with an association, then you will have problems.
My suggestion is that you should not have a model called an "attribute" - call it something else, eg "properties", and the problems will go away.