I am using the PublicActivity gem and this was created in my database
schema.db
create_table "activities", :force => true do |t|
t.integer "trackable_id"
t.string "trackable_type"
t.integer "owner_id"
t.string "owner_type"
t.string "key"
t.text "parameters"
t.integer "recipient_id"
t.string "recipient_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I am able to track all posts that are happening inside my website by using this inside the post.rb model
class Post < ActiveRecord::Base
include PublicActivity::Model
tracked except: :destroy, owner: ->(controller, model) { controller && controller.current_user }
end
By doing this, I can get the owner_id. What do I need to set recipient: to make it grab the value? Right now recipient_id is nil.
I want to ultimately use something like this in my view <%= link_to activity.recipient_id.name %> to get the name of the recipient where the activity was made to
I'm trying to set it to either the post_id or user_id but I'm getting an undefined local variable or method error.
Here's the table of the model that I'm tracking
create_table "postcomments", :force => true do |t|
t.text "content"
t.integer "user_id"
t.integer "post_id"
t.timestamp "created_at", :null => false
t.timestamp "updated_at", :null => false
t.text "comment_content"
end
I put this in the Model
assuming recipient_id is the user of tracked object. Otherwise change model.user as per your application
tracked recipient: ->(controller, model) { model && model.user }
Related
I have two models, Job and Survey. I am using the PaperTrail gem to track changes that occur on the models, and I want to save the job_id onto the version every time it’s created.
# app/models/job.rb
class Job < ActiveRecord::Base
has_paper_trail :ignore => [:id, :created_at, :updated_at],
:meta => { :resource_id => self.id }
has_one :survey
end
# app/models/survey.rb
class Survey < ActiveRecord::Base
has_paper_trail :ignore => [:id, :created_at, :updated_at],
:meta => { :resource_id => self.job_id }
belongs_to :job
end
# app/db/schema.db
# versions
create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
t.text "object_changes"
t.integer "resource_id"
end
# surveys
create_table "surveys", force: :cascade do |t|
t.string "survey"
t.integer "job_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
When I run this code and try to save a new record, I get the following error:
undefined method `job_id' for #<Class:0x007fd53d70c0a0>
How do I correctly fetch the foreign job_id key from inside my Survey model?
Solved by using :job_id instead of self.job_id.
Googling for this, I see the Rails core team is working on a solution for Rails 4 but that's a ways away.
Both Users and Circles have a has_and_belongs_to_many relationship to the other.
My schema looks like this:
create_table "circles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "circles_users", :force => true do |t|
t.integer "circle_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password"
end
But in my code, in my circles_controller when I try to do this:
def create
#circle = Circle.new(params[:circle])
#circle.users << User.find(session[:user].id)
...
I get the following error in my browser:
SQLite3::ConstraintException: circles_users.created_at may not be NULL: INSERT INTO "circles_users" ("circle_id", "user_id") VALUES (11, 5)
What should I do about created_at and updated_at being false here?
I ended up getting it to work by removing the timestamps from the migration that was used to create the join table.
The right schema should be like this:
create_table "circles_users", :force => true do |t|
t.integer "circle_id"
t.integer "user_id"
end
try this
def new
#circle = Circle.new
1.times { #circle.users.build } if #circle.users.nil?
end
def create
#circle = Circle.new(params[:circle])
#circle.update_attributes(session[:user]) # this will automatically update location table by using relationship
#circle.save
end
see HABTM
Just give this a try
def create
#circle = Circle.new(params[:circle])
#circle.users = [User.find(session[:user].id)]
Might not work, I have not tried it locally, sry abt that ^_^
In these datetime fields you should insert current timestamp or change them to nullable fields if you not filling them with data.
I have the following models:
class Constraint < ActiveRecord::Base
belongs_to :constraint_category
end
class ConstraintCategory < ActiveRecord::Base
has_many :constraints
end
The models have these attributes (from db/schema.rb):
create_table "constraint_categories", :force => true do |t|
t.string "value"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "active"
end
create_table "constraints", :force => true do |t|
t.string "phrase"
t.integer "constraint_category_id", :limit => 255
t.boolean "active"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I would like to create a query that finds all constraints where the "active" attribute is "true" and the "constraint_category.value" is "Noun".
Would love any advice on getting there.
Constraint.joins(:constraint_category).where('constraints.active = ? and constraint_categories.value = ?', true, 'Noun')
See conditions and joins in the guide.
Here's my schema file..
ActiveRecord::Schema.define(:version => 20120505115340) do
create_table "clients", :force => true do |t|
t.string "name"
t.string "detail"
t.string "more_detail"
t.string "more_details"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "jobs", :force => true do |t|
t.string "name"
t.integer "number"
t.string "responsible"
t.string "monthly"
t.string "quarterly"
t.string "other"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
And here's my migration file's..
class CreateClients < ActiveRecord::Migration
def change
create_table :clients do |t|
t.string :name
t.string :detail
t.string :more_detail
t.string :more_details
t.timestamps
end
end
end
class CreateJobs < ActiveRecord::Migration
def change
create_table :jobs do |t|
t.string :name
t.integer :number
t.string :responsible
t.string :monthly
t.string :quarterly
t.string :other
t.timestamps
end
end
end
In my view file, I have it setup so that is pulls out the client.name and shows it to the user <%= link_to client.name, client_path(client) %>.
However, all im getting back when I create a new entry is /clients/1 instead of the name that I specified in my form.
When I try to migrate the DB nothing happens and then when I try to drop he DB to start afresh it tells me that it does even exist.
If I understand you correctly, you are concerned that your view displays a link to /clients/1 for your newly created object?
This is the default path when using Ruby on Rails, and is what will be produced by the path helper object_path(object) that you are using. This can be customized (see guides on routes.rb). If this is not a problem, then your application is working as intended.
BtW, the number used in the default path refers to the id given to the object. All objects stored using ActiveRecord will automatically get a unique id which can be used to identify the object. Just as the created_at and updated_at columns in your schema, the id column will be created regardless if you explicitly define it in your schema or not.
To reset your database (drop, recreate and migrate to current schema), use the following command:
rake db:reset
EDIT:
<%= link_to client.name, client_path(client) %>
Should result in the following HTML (where CLIENT_NAME is the name attribute of the client)
CLIENT_NAME
I have two models, Zombie and Tweet. Schema below:
create_table "tweets", :force => true do |t|
t.string "status"
t.integer "zombie_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "zombies", :force => true do |t|
t.string "name"
t.string "graveyard"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
With the following associations:
class Zombie < ActiveRecord::Base
has_many :tweets
end
class Tweet < ActiveRecord::Base
belongs_to :zombie
end
In the Zombie#show view, I have added a "New Tweet" button sending it to Tweet#new (new_tweet_path). In the Tweet#new view I have a form with two fields: status and zombie_id. When I arrive at the Tweet#new page coming from a Zombie's profile, I don't want to have to fill in the zombie_id, or I'd like it to know what the id is since I just came from it's profile on the previous page.
What do I need to do to accomplish this? I'm assuming I need to send the zombie object from the Zombie#show page to the Tweet#new page, but I'm not sure what I need to do in the controller or views to handle this. Any advice?
In the Zombie#show view add zombie_id param to the new_tweet_path call like this:
new_tweet_path(zombie_id: #zombie.id)
Then in the Tweet#new create a Tweet model with already filled zombie_id, that was passed in params hash:
#tweet = Tweet.new(zombie_id: params[:zombie_id])