Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom db fields #55

Closed
mbajur opened this issue Jan 11, 2013 · 8 comments
Closed

Custom db fields #55

mbajur opened this issue Jan 11, 2013 · 8 comments

Comments

@mbajur
Copy link

mbajur commented Jan 11, 2013

Hello,
first of all - thank you for creating this amazing gem, you saved me a lot of work and i really appreciate it :)

Anyway, i have a small problem. I'm creating an app - let's call it an online restaurants catalogue. There are two models: Spot and City

What i need to do is to be able to display an activity stream on single Spot's profile but i also want to display activity streams on City's profiles (activity streams of spots which belongs to this city, not city's streams itself). Offcourse i can use params parameter for create_activity method when tracking Spot activity but it would be probably not quite optimal to query City's activity using SQL LIKE statements (they are quite heavy, right?). I decided it would better to add a column to activities table in which i will keep the city ID.

Is there any way to use custom table columns without hacking a public_activity source code ? Unfortunatelly, this code won't work:

@spot.create_activity key: 'spot.update', city_id: @spot.city.id

Thank you for any help!

@pokonski
Copy link
Member

Glad you like it!

You are right, querying a serialized column would be a pain, so it is highly advised against doing that.

In the early days of the gem I also was adding custom columns. The solution circumvents parts of the gem, but if you absolutely need it can be done like this:

  1. Create a migration which adds desired columns to activities table
  2. Skip using #create_activity method, and access the Activity model directly:
PublicActivity::Activity.create key: 'spot.update', trackable: @spot, params: {...}, city_id: @spot.city_id, owner: current_user

Please note that this completely ignores your defaults set in Spot model (inside the #tracked method), so you are on your own with setting :owner or other defaults you have.

Note to self: this might be a good idea to allow passing custom column values to #create_activity.

EDIT: Included :trackable attribute which I forgot.

@stas
Copy link
Contributor

stas commented Jan 11, 2013

Lately I see a lot of people asking question about how to use this gem.
I suggest we get a mailing list or something. Since a lot of people receive such emails which are not relevant to the development.

@pokonski
Copy link
Member

@stas good idea.

@mbajur
Copy link
Author

mbajur commented Jan 12, 2013

Thank you for your help @pokonski , work's great! :)

@mbajur mbajur closed this as completed Jan 12, 2013
@farnoy farnoy reopened this Jan 12, 2013
@farnoy
Copy link
Contributor

farnoy commented Jan 12, 2013

I will implement this and inform you on the progress here.

@farnoy
Copy link
Contributor

farnoy commented Jan 12, 2013

This works well, I think, but two things are needed: migration and whitelist addition for the attribute.

@farnoy
Copy link
Contributor

farnoy commented Jan 12, 2013

This is definitely coming to 1.0

@owenstrevor
Copy link

After searching for a while, this worked for me when I wanted to add a custom field which is a reference to a model with a uuid as the id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants